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
webcan
Forum Commoner
Posts: 66 Joined: Tue Oct 28, 2003 2:25 pm
Location: Toronto, Canada
Post
by webcan » Mon Mar 29, 2004 11:16 pm
Can someone tell me why this doesn't do anything?
[Admin Edit: changed tags to
tags][/color][/size]
Code: Select all
$speakerlist = "John Abrams+& Lucille Travis";
preg_replace("/ /", "+", $speakerlist);
preg_replace("/\046/", "and", $speakerlist);
(The second one searches for ampersands)
No changes are made to $speakerlist.
Thanks,
Peter.
twigletmac
Her Royal Site Adminness
Posts: 5371 Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK
Post
by twigletmac » Tue Mar 30, 2004 1:46 am
You need to capture the returned value from the preg_replace() into the $speakerlist variable:
Code: Select all
$speakerlist = preg_replace("/ /", "+", $speakerlist);
$speakerlist = preg_replace("/\046/", "and", $speakerlist);
On a side note, instead of multiple preg_replace() calls you can do one using arrays:
Code: Select all
$string_from = array('/ /', '/\046/');
$string_to = array('+', 'and');
$speakerlist = preg_replace($string_from, $string_to, $speakerlist);
Mac
redmonkey
Forum Regular
Posts: 836 Joined: Thu Dec 18, 2003 3:58 pm
Post
by redmonkey » Tue Mar 30, 2004 5:48 am
I see no need to envoke the regex engine for this case. Therefore str_replace would be a more appropriate function for this application.
twigletmac
Her Royal Site Adminness
Posts: 5371 Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK
Post
by twigletmac » Tue Mar 30, 2004 6:02 am
redmonkey wrote: I see no need to envoke the regex engine for this case. Therefore str_replace would be a more appropriate function for this application.
Doh, totally missed that very obvious point
.
Mac