I need to find one of the three words even if written at the beginning of the string and not just in the middle or at the end. This is my code:
<?php
$string = "one test";
$words = array( 'one', 'two', 'three' );
foreach ( $words as $word ) {
if ( stripos ( $string, $word) ) {
echo 'found<br>';
} else {
echo 'not found<br>';
}
}
?>
If $string is "one test" the search fails; if $string is "test one" the search is good.
Thank you!
stripos
can return a value which looks like false
, but is not, i.e. 0
. In your second case, the word "one"
matches "one test"
at position 0 so stripos
returns 0, but in your if
test that is treated as false. Change your if
test to
if ( stripos ( $string, $word) !== false ) {
and your code should work fine.