Forum Moderators: coopster
stristr for a really simple check of a string to see if it contains the text "foo". I think I'm missing something obvious, but how can I check to see whether a string contains either "foo", "bar" or "baz"? I tried pipes but that doesn't seem to work. Can I use stristr or is there another function I've overlooked? Thanks for your help!
I'm a C guy - somebody pointed this out to me before...
[us2.php.net...]
Edit Add:
Sorry, I think I answered the wrong question. I didn't read your headline.
In C you could look for the string in three seperate lines using if/else.
<added>
I just realized you were using stristr() and I was thinking stripos(). stripos() is a PHP5 function, that is why I commented on the performance. Still, a preg_match is going to work well here for you.
</added>
<pre>
<?php
$strings = array(
'this string',
"this\nstring\n",
"thisfoostring",
"this\nbaz\nstring",
"this foo bar baz string",
"this bazstring"
);
foreach ($strings as $string) {
if (preg_match_all("/foo¦bar¦baz/is", $string, $matches)) {
print htmlentities($string) . "\n";
print_r($matches);
print "\n\n";
}
}
?>
</pre>