Skip to content

PHP: Check if a string contains a specific word.

You can use PHP’s built-in function called str_contains to check if the given word exists in a string or not. If it finds the word it will return true otherwise it will return false. This function is case-sensitive.

PHP 8: str_contains function

<?php
$word = "fox";
$mystring = "The quick brown fox jumps over the lazy dog";
 
// Test if string contains the word 
if(str_contains($mystring, $word)){
    echo "Word Found!";
} else{
    echo "Word Not Found!";
}
?>

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments