php logo on purple background

PHP: How to check if a string contains a specific word?

By

in

When working with strings in PHP, you can already use many useful functions to manipulate the string contents. However, sometimes you only want to check for the string contents before performing an action.

In PHP you can determine if a string includes a word by utilizing functions like strpos(), preg_match() or str_contains().

Using strpos()

The strpos() function provides the position of the instance of a substring within a string. If the substring is not found it returns false. You can use this function to confirm whether a string contains a word, in this manner:

PHP
$string = 'This is a sample string.';
$word = 'sample';

if (strpos($string, $word) !== false) {
    echo 'The string contains the word.';
} else {
    echo 'The string does not contain the word.';
}

In this example, we utilize the strpos() function to verify whether the $string includes the $word. If the $word is present in the $string, the function will provide a position value resulting in the message “The string contains the word.” Conversely if the function returns false it will display “The string does not contain the word.”

Using preg_match()

The preg_match() function scans a string for a pattern. It gives back true if the pattern is detected or false if not. This function can be utilized to verify whether a specific word is present in a string:

PHP
$string = 'This is a sample string.';
$word = 'sample';
$pattern = '/' . $word . '/i';

if (preg_match($pattern, $string)) {
    echo 'The string contains the word.';
} else {
    echo 'The string does not contain the word.';
}

Using str_contains()

PHP 8 introduces the method str_contains(). You can use this method to do exactly what this post is about: check if a string contains a specific word. The syntax is as follows:

PHP
$string = 'This is a sample string.';
$word = 'sample';

if (str_contains($string, $word)) {
    echo 'The string contains the word.';
} else {
    echo 'The string does not contain the word.';
}

Performance Comparison

When comparing the performance of those three methods, we get the following result:

MethodExecution Time
strpos()0.137 µs
preg_match()0.154 µs
str_contains()0.128 µs

So the new method str_contains() (for PHP 8.0 or higher) is the fastest one.

For this test, the mean execution time in a loop with 1,000,000 cycles was calculated for each method.

Conclusion

The solutions discussed above work in most of the cases. But to be honest, the solutions do not really check for a “complete” word, they also return true for parts of a word (sub-strings):

PHP
$string = 'This is an example string.';
$word = 'ample'; // not a complete word, just a sub-string
$pattern = '/' . $word . '/i';

if (preg_match($pattern, $string)) {
    echo 'The string contains the sub-string.';
} else {
    echo 'The string does not contain the sub-string.';
}

This example will return true: The string contains the sub-string..

To make the solutions more robust and to really find complete words only, you need to use preg_match() and check for boundary characters before and after the word. Which means: simply use \b to perform a “whole words only” search. An example:

PHP
$pattern = '/\b' . $word . '\b/i';

or, if your $word may contain regex-meta-chars, do:

PHP
$pattern = '/\b' . preg_quote($word) . '\b/i';


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *