str_replace help

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
doveready
Forum Newbie
Posts: 3
Joined: Thu Oct 07, 2010 10:19 am

str_replace help

Post 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!
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: str_replace help

Post by twinedev »

You may want to just us the urlencode() function.

see http://us2.php.net/urlencode

-Greg
doveready
Forum Newbie
Posts: 3
Joined: Thu Oct 07, 2010 10:19 am

Re: str_replace help

Post by doveready »

Thanks, but in this case I need to do it as requested. Is there a way to add to the string?
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: str_replace help

Post by Jonah Bron »

Why? What are you doing that urlencode can't?
doveready
Forum Newbie
Posts: 3
Joined: Thu Oct 07, 2010 10:19 am

Re: str_replace help

Post 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?
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: str_replace help

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