Page 1 of 1
preg_replace doesn't do anything?
Posted: Mon Mar 29, 2004 11:16 pm
by webcan
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.
Posted: Tue Mar 30, 2004 1:46 am
by twigletmac
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
Posted: Tue Mar 30, 2004 5:48 am
by redmonkey
I see no need to envoke the regex engine for this case. Therefore str_replace would be a more appropriate function for this application.
Posted: Tue Mar 30, 2004 6:02 am
by twigletmac
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