str_replace

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
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

str_replace

Post by d3ad1ysp0rk »

I'm sorry, but I read both the php.net and Zend articles and comments on str_replace, but I just can't seem to understand it..

can anyone explain what goes into the parenthesis?

ie, mail() is mail(who its to, the message, the headers)

what would str_replace() be?


ThanKs
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Post by Cruzado_Mainfrm »

str_replace("what you look for","the substring that is going to replace 'what you look for'",$TheStringThatContainsEverything);

ie.:

Code: Select all

<?php
$greeting = "Hi my name is John";

str_replace("Earl","John",$greeting);

echo $greeting; //will return: Hi my name is Earl

?>
good luck
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

k thanks a lot :D
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

What about replacing 's?

I have a variable called $varsign and I want to get rid of all the 's and change them to ' (the HTML number for them) so i dont get this:

"John's cat is very fast."
becomes
"John''s cat is very fast."

yet it seems to not replace it..

my code:

Code: Select all

str_replace("'","'",$varsign);
Thanks
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

str_replace returns the new string with the replacements. So you have to assign it to something, usually the original string.

Code: Select all

$varsign = str_replace("'", ''', $varsign);
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You don't need str_replace() to get rid of escape characters (the backslashes), use stripslashes() instead, or turn off magic_quotes in your php.ini.

Mac
Post Reply