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.
Simple search and replace function issue
Moderator: General Moderators
-
ViserExcizer
- Forum Newbie
- Posts: 24
- Joined: Tue Nov 25, 2008 1:17 pm
Re: Simple search and replace function issue
You can either use parse_url: http://us2.php.net/function.parse-url
or str_replace: http://us3.php.net/str_replace
or both.
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
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
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.
Returned:
http://www.something.com/index.php?userid=123456
Code: Select all
$uid = "123456";
$x = "www.something.com/index.php?userid={userid}";
$y = str_replace("{userid}", $uid, $x);
echo $y;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
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.Returned:Code: Select all
$uid = "123456"; $x = "www.something.com/index.php?userid={userid}"; $y = str_replace("{userid}", $uid, $x); echo $y;
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.