Page 1 of 1
str_replace help
Posted: Thu Oct 07, 2010 10:26 am
by doveready
Hello All,
I'm using the following code to replace a "space" with a "+" in a canonical link:
Code: Select all
<?php echo str_replace (" ","+",$desc); ?>
Within that same string, can I add more: such as to replace "," with "%2C" and "/" with "%2F"?
How would I integrate those srt_replace commands within the existing string?
Any help would be greatly appreciated! Thanks!
Re: str_replace help
Posted: Thu Oct 07, 2010 10:40 am
by twinedev
You may want to just us the urlencode() function.
see
http://us2.php.net/urlencode
-Greg
Re: str_replace help
Posted: Thu Oct 07, 2010 12:07 pm
by doveready
Thanks, but in this case I need to do it as requested. Is there a way to add to the string?
Re: str_replace help
Posted: Thu Oct 07, 2010 12:10 pm
by Jonah Bron
Why? What are you doing that urlencode can't?
Re: str_replace help
Posted: Thu Oct 07, 2010 12:16 pm
by doveready
To be honest, i'm not that good with PHP. It would be easier just to add to the string then trying to figuere out the url encode. So, can I add this to the string? Is it even possible?
Re: str_replace help
Posted: Thu Oct 07, 2010 12:39 pm
by Jonah Bron
No figuring out needed. It takes a string and encodes it to a URL-safe form, which is what you're trying to do. Here's how to use it.
Code: Select all
$string = "I am, w/ out";
$output = urlencode($string);
echo $output; //outputs "I+am%2C+w%2F+out"
That's what you want, right?