Possible Duplicate:
Regular expression: match all words except
I need your help for using Regex in PHP to negate a selection. So I have a string like this :
“Hello my name is tom”
What I need to do is to delete everything from this string witch is not “tom” or “jack” or “alex” so I tried :
$MyString = "Hello my name is tom"
print_r(preg_replace('#^tom|^jack|^alex#i', '', $MyString));
But it’s not working…
Can you help me with that ?
Thanks
If you want to delete everything except something, may be it’s better done the other way around: capture the something only? For example…
What you’ve tried to do is use a character class syntax ([
^s]will match any character but s). But this doesn’t work with series of characters, there’s no such thing as ‘word class’. )