How do I check if a string contains a specific word in PHP

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

Consider:

$a =  How are you? ;

if ($a contains  are )
    echo  true ;

Suppose I have the code above, what is the correct way to write the statement if ($a contains are )?

Answers

http://php.net/manual/en/function.strpos.php http://php.net/manual/en/function.strpos.php

$a =  How are you? ;

if (strpos($a,  are ) !== false) {
    echo  true ;
}

Note that the use of !== false is deliberate; strpos returns either the offset at which the needle string begins in the haystack string, or the boolean false if the needle isn t found. Since 0 is a valid offset and 0 is "falsey", we can t use simpler constructs like !strpos($a, are ).

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/4366730/how-do-i-check-if-a-string-contains-a-specific-word-in-php

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils