473,386 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

having trouble with string replacement

Hello,

I have strings on my page of the form

param1=abcd&agency_id=1234&param2=dex

I want to write a PHP expression to take strings similar to the above,
and output

param1=abcd&agency_id=<?php echo $LTON; ?>&param2=dex

How can I do this?

Thanks, - Dave

Nov 4 '05 #1
8 1239
You just did!

I don't get it. What's the problem?

Nov 4 '05 #2
la***********@zipmail.com wrote:
Hello,

I have strings on my page of the form

param1=abcd&agency_id=1234&param2=dex

I want to write a PHP expression to take strings similar to the above,
and output

param1=abcd&agency_id=<?php echo $LTON; ?>&param2=dex

How can I do this?

Thanks, - Dave


Hi Dave,

Looks fine to me.
What is your problem?

What does it produce that you didn't expect?
Does $LTON have a value?

Regards,
Erwin Moller
Nov 4 '05 #3
I actually want the result string (what is printed to the browser) to
look like

param1=abcd&agency_id=<?php echo $LTON; ?>&param2=dex

This PHP code:

<?php
$str = "&mine=20&agency_id=234&test=abc";
$search = "/agency_id=\\d+/i";
$replace = "agency_id=<?php echo \$LTON; ?>";
$retVal = preg_replace($search, $replace, $str);
print $retVal;
?>

regrettably produces the wrong result

&mine=20&agency_id=&test=abc

How can I heal the pain? Thanks, - Dave

Nov 4 '05 #4
la***********@zipmail.com wrote:
I actually want the result string (what is printed to the browser) to
look like

param1=abcd&agency_id=<?php echo $LTON; ?>&param2=dex

This PHP code:

<?php
$str = "&mine=20&agency_id=234&test=abc";
$search = "/agency_id=\\d+/i";
$replace = "agency_id=<?php echo \$LTON; ?>";
$retVal = preg_replace($search, $replace, $str);
print $retVal;
?>

regrettably produces the wrong result

&mine=20&agency_id=&test=abc

How can I heal the pain? Thanks, - Dave


Hi Dave,

The answer is actually very simple:
Your desired line:
param1=abcd&agency_id=<?php echo $LTON; ?>&param2=dex

is not a legal url.

You should urlencode all paramvalues before using them in an URL.

So try something like:
$theURL = "?param1=".urlencode("abcd");
// not very usefull, but you should do it with values
// that possible contain difficult character.

$theURL .= "&agency_id=".urlencode("<?php echo $LTON; ?>");

$theURL .= "&param2=".urlencode("abc");
That should work.
Check your results by starting your script with:

<pre>
<? print_r($_GET); ?>
</pre>
It will spit out all name/value-pairs.

Good luck,
Erwin Moller
Nov 7 '05 #5
Thanks for your reply. However, know that what is happening is that
the result of the search and replace will be output to another PHP
file, which will then be interpreted. So

param1=abcd&agency_id=<?php echo $LTON; ?>&param2=dex

is the output that I want to go to the PHP file. That file will then
be interpreted, and the legal URL will be formed from that.

Sorry I didn't mention that earlier. Thanks for any further
assistance, - Dave

Nov 7 '05 #6
Just try to put what you want directly into the address bar,
and you'll see (most ?) browsers will translate it to an url-safe
address, which means
<?php echo $LTON; ?>
becomes
%3C?php%20echo%20$LTON;%20?%3E

Greetings Frizzle.

Nov 7 '05 #7
<la***********@zipmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
<?php
$str = "&mine=20&agency_id=234&test=abc";
$search = "/agency_id=\\d+/i";
$replace = "agency_id=<?php echo \$LTON; ?>";
$retVal = preg_replace($search, $replace, $str);
print $retVal;
?>


I can't see how you have it set up, but if a previous page does not pass the
variable $LTON, it will of course equal nothing. Also, you are putting
<?php ?> instead of <?php ?> tags.

This code works:

<?php
$LTON = 1;
$str = "&mine=20&agency_id=234&test=abc";
$search = "/agency_id=\d+/i";
$replace = "agency_id=" . $LTON;
$retVal = preg_replace($search, $replace, $str);
print $retVal;
?>

David
Nov 7 '05 #8
> I can't see how you have it set up, but if a previous page does not pass
the variable $LTON, it will of course equal nothing. Also, you are
putting <?php ?> instead of <?php ?> tags.


^^^ I meant "inside".
Nov 7 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Juha Suni | last post by:
Hi! I have managed to live without using too much regular expressions so far, and now that I need one, I need some help too. I have a string containing a (possibly large) block of html. I need...
2
by: Stephen Horne | last post by:
Just recently I decided I want to make use of my ISP freebie webspace. In order to make that easier, I'd like to be able to automatically synchronise an FTP file/folder heirarchy with one on my...
5
by: Roose | last post by:
How can I do a "".replace operation which tells me if anything was actually replaced? Right now I am doing something like: if searchTerm in text: text = text.replace( searchTerm, 'other' ) ...
11
by: Christopher Benson-Manica | last post by:
Let's say I have a std::string, and I want to replace all the ',' characters with " or ", i.e. "A,B,C" -> "A or B or C". Is the following the best way to do it? int idx; while(...
35
by: jacob navia | last post by:
Hi guys! I like C because is fun. So, I wrote this function for the lcc-win32 standard library: strrepl. I thought that with so many "C heads" around, maybe we could improve it in a...
29
by: zoltan | last post by:
Hi, The scenario is like this : struct ns_rr { const u_char* rdata; }; The rdata field contains some fields such as :
2
by: josh | last post by:
Hi, I am trying to validate cXML documents against cXML.dtd using the XmlValidatingReader. If I set the XMLValidatingReader's ValidatingType to ValidationType.DTD, I get the following...
21
by: gary | last post by:
How would one make the ECMA-262 String.replace method work with a string literal? For example, if my string was "HELLO" how would I make it work in this instance. Please note my square...
5
by: int main(void) | last post by:
Hi all, Following is my attempt to write a string search and replace function. #include <stdio.h> #include <stdlib.h> #include <string.h>...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.