regexp help

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

Post Reply
boinnk
Forum Newbie
Posts: 22
Joined: Fri Jul 26, 2002 9:53 am
Location: Sweden

regexp help

Post 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?
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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.
boinnk
Forum Newbie
Posts: 22
Joined: Fri Jul 26, 2002 9:53 am
Location: Sweden

Post by boinnk »

That's not really what I'm looking for, it's more like how to exclude a word from the regexp query.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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?
boinnk
Forum Newbie
Posts: 22
Joined: Fri Jul 26, 2002 9:53 am
Location: Sweden

Post by boinnk »

Perhaps that will do the job, I have to do some testing...
Thanks alot anyways
Post Reply