Inserting ? into regex pattern
Posted: Sun Oct 14, 2007 6:28 am
I've just been having a look at some code in another post.
Still no go. Pretty basic , I know, but it's better to ask than to go on not knowing.
How can I process ? for insertion into a regex pattern?
Thanks
It seems to work like a champ, but I tried to change $original from 'nice' to '?' and the call to r() likewise. It throws an error. I figure because ? has it's own meaning in regex. So I thought I'd call it with preg_quote() ...Kieran Huggins wrote:Code: Select all
$original = "It's a nice day in a nice way."; $values = array('glorious','wonderful','marvelous','good','grand'); function r($needle,$replacement_needles,$haystack){ $haystack = preg_replace('/\b'.$needle.'\b/i',$replacement_needles[array_rand($replacement_needles)],$haystack,1); if (strpos($haystack,$needle)) $haystack = r($needle,$replacement_needles,$haystack); return $haystack; } echo r('nice',$values,$original);
Code: Select all
echo r(preg_quote('?'),$values,$original);How can I process ? for insertion into a regex pattern?
Thanks