single regexp to check for several words?
Posted: Sat Dec 13, 2008 4:46 am
Is there a way I can check for the presence of several words with a single regexp?
For example, how could I check if a string contains 'red', 'green' and 'blue'? (each at least once)
The only way I could think of was something like this: (checking for all possible orders)
But that's obviously dumb, I'd prefer a method that can also be used for 20 words instead of 3 
For example, how could I check if a string contains 'red', 'green' and 'blue'? (each at least once)
The only way I could think of was something like this: (checking for all possible orders)
Code: Select all
preg_match("/(red.*green.*blue|red.*blue.*green|green.*red.*blue|green.*blue.*red|blue.*red.*green|blue.*green.*red)/",$s);