Page 1 of 1

Simple search and replace function issue

Posted: Tue Nov 25, 2008 1:20 pm
by ViserExcizer
Not so much a question, but i need to know which php function can be used to process this type of string:
lets say i have a URL, echoed within the html on a php page,
but at the end of the URL is a id

http://www.yourdomain.com/?id=123456

the idea is im going to write it like this with the curly brackets

http://www.yourdomain.com/?id={userid}

so, there wont be any specific user id,

The question is which, php function, can search all the {userid} part of the URLS contained within a page , and replace it with a . $row['userid'] . or any other variable from a database of my choosing?

Basically, i just need to replace the whole {userid} including the curly brackets into something else... thanks in advance.

Re: Simple search and replace function issue

Posted: Tue Nov 25, 2008 1:32 pm
by sparrrow
You can either use parse_url: http://us2.php.net/function.parse-url
or str_replace: http://us3.php.net/str_replace
or both.

Re: Simple search and replace function issue

Posted: Tue Nov 25, 2008 1:49 pm
by ViserExcizer
sparrrow wrote:You can either use parse_url: http://us2.php.net/function.parse-url
or str_replace: http://us3.php.net/str_replace
or both.

thanks for your answer, but the problem is , The string i want to replace is the whole {userid}, not just the userid, but {userid} with the curly brackets.

so
http://yourdomain.com/?id={userid}

will look like this

http://yourdomain.com/?id=123456

Wont the curly brackets cause any problem when entered into an array function in str_replace?

Re: Simple search and replace function issue

Posted: Tue Nov 25, 2008 2:05 pm
by sparrrow
Curly brackets within a string and not containing php code do not need escaped. I tried the following code to be sure and it worked.

Code: Select all

$uid = "123456";
$x = "www.something.com/index.php?userid={userid}";
$y = str_replace("{userid}", $uid, $x);
echo $y;
Returned:

http://www.something.com/index.php?userid=123456

Re: Simple search and replace function issue

Posted: Tue Nov 25, 2008 2:12 pm
by ViserExcizer
sparrrow wrote:Curly brackets within a string and not containing php code do not need escaped. I tried the following code to be sure and it worked.

Code: Select all

$uid = "123456";
$x = "www.something.com/index.php?userid={userid}";
$y = str_replace("{userid}", $uid, $x);
echo $y;
Returned:

http://www.something.com/index.php?userid=123456

okay thanks, you've solved that problem for me, i could have tested it but my WAMP server is down, thanks again.