Q:

php check if string contains word

$myString = 'Hello Bob how are you?';
if (strpos($myString, 'Bob') !== false) {
    echo "My string contains Bob";
}
25
$a = 'How are you?';

if (strpos($a, 'are') !== false) {
    echo 'true';
}
2
if (strpos($string, 'substring') !== false) {
	// do stuff 
}
3
$a = 'How are you?';

if (strpos($a, 'are') !== false) {
    echo 'true';
}
0
if (strpos($haystack,$needle) !== false) {
    echo "$haystack contains $needle";
}
1
preg_match('/(http|ftp|mailto)/', $string, $matches);
var_dump($matches);
1
// returns true if $needle is a substring of $haystack
function contains($haystack, $needle){
    return strpos($haystack, $needle) !== false;
}
2
if (preg_match('/[A-Za-z]/', $myString) && preg_match('/[0-9]/', $myString))
{
    echo 'Contains at least one letter and one number';
}
0

New to Communities?

Join the community