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
boinnk
Forum Newbie
Posts: 22 Joined: Fri Jul 26, 2002 9:53 am
Location: Sweden
Post
by boinnk » Thu May 15, 2003 3:13 pm
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?
patrikG
DevNet Master
Posts: 4235 Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK
Post
by patrikG » Thu May 15, 2003 4:35 pm
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.
boinnk
Forum Newbie
Posts: 22 Joined: Fri Jul 26, 2002 9:53 am
Location: Sweden
Post
by boinnk » Thu May 15, 2003 5:08 pm
That's not really what I'm looking for, it's more like how to exclude a word from the regexp query.
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Thu May 15, 2003 5:47 pm
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?
boinnk
Forum Newbie
Posts: 22 Joined: Fri Jul 26, 2002 9:53 am
Location: Sweden
Post
by boinnk » Thu May 15, 2003 6:15 pm
Perhaps that will do the job, I have to do some testing...
Thanks alot anyways