php string contains substring

203

php string contains -

$string = 'The lazy fox jumped over the fence';

if (str_contains($string, 'lazy')) {
    echo "The string 'lazy' was found in the string\n";
}

How do I check if a string contains a specific word php -

$a = 'How are you?';

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

how to check if a string contains a substring in php -

$a = 'How are you?';

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

check if string contains character php -

// this method is new with PHP 8 and above
$haystack = "Damn, I wonder if this string contains a comma.";
if (str_contains($haystack, ",")) {
	echo "There is a comma!!";
}

Comments

Submit
0 Comments