Simple search and replace function issue

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ViserExcizer
Forum Newbie
Posts: 24
Joined: Tue Nov 25, 2008 1:17 pm

Simple search and replace function issue

Post 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.
sparrrow
Forum Commoner
Posts: 81
Joined: Mon Oct 20, 2008 12:22 pm

Re: Simple search and replace function issue

Post 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.
ViserExcizer
Forum Newbie
Posts: 24
Joined: Tue Nov 25, 2008 1:17 pm

Re: Simple search and replace function issue

Post 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?
sparrrow
Forum Commoner
Posts: 81
Joined: Mon Oct 20, 2008 12:22 pm

Re: Simple search and replace function issue

Post 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
ViserExcizer
Forum Newbie
Posts: 24
Joined: Tue Nov 25, 2008 1:17 pm

Re: Simple search and replace function issue

Post 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.
Post Reply