Page 1 of 1

regexp help

Posted: Thu May 15, 2003 3:13 pm
by boinnk
How do I find/replace all values that doesn't match "foo"?

Code: Select all

<?php
preg_replace("/not foo/i","hello","one two three foo");
?>
Should print:
"hello hello hello foo"

Anyone?

Posted: Thu May 15, 2003 4:35 pm
by patrikG

Code: Select all

<?php
preg_replace("/(.)*foo/i","hello","one two three foo"); 
?>
will return all characters (not just a-z, A-Z and 0-9) that are not foo.

Posted: Thu May 15, 2003 5:08 pm
by boinnk
That's not really what I'm looking for, it's more like how to exclude a word from the regexp query.

Posted: Thu May 15, 2003 5:47 pm
by volka
is

Code: Select all

<?php
function myReplace($elem)
{
	if ($elem != 'foo') // probably more
		return 'hello';
	else
		return $elem;
}

$text = "one two three foo";

$text = join(' ', array_map('myReplace', explode(' ', $text)));

echo $text;
?>
sufficient?

Posted: Thu May 15, 2003 6:15 pm
by boinnk
Perhaps that will do the job, I have to do some testing...
Thanks alot anyways