How Can I Check if String Contains a Specific Word or Substring?
Let’s say you have a string and you want to check if it contains a specific word, character or substring. There are a lot of things that you need to be aware of in order to do the check properly without making silly mistakes. For example, using !=
instead of !==
during the check can sometimes result in false negatives. Similarly, you might be looking for a word in a case-insensitive manner but forget that the method you used is case-sensitive.
In this tutorial, you will learn about different methods to check if a particular word, character or substring exists in another string. We will also discuss the pros and cons of each of these methods in detail.
Using strpos() to Check if String Contains Substring [Case Sensitive]
The easiest way to check if a string contains a specific word is with the help of PHP strpos() function. PHP strpos()
function returns the position of the first occurrence of a substring in a string. It returns FALSE if the word or substring was not found. This means that you can compare the return value of strpos()
to FALSE to check for a substring. The following example will show it in action.
PHP
$the_string = "I am 5 years older than you.";
$the_word = "years";
$the_character = "I";
$the_substring = "5 years";
// Output — The word "years" exists in given string.
if (strpos($the_string, $the_word) !== false) {
echo 'The word "'.$the_word.'" exists in given string.';
}
// Output — The character "I" exists in given string.
if (strpos($the_string, $the_character) !== false) {
echo 'The character "'.$the_character.'" exists in given string.';
}
// Output — The substring "5 years" exists in given string.
if (strpos($the_string, $the_substring) !== false) {
echo 'The substring "'.$the_substring.'" exists in given string.';
}
You should note that I have used the strict inequality operator (!==
). If the word we are looking for occurs at the beginning of the string, strpos()
will return 0 which will evaluate to FALSE value with !=
operator. Here is an example:
PHP
// Output — The character "I" does not exist in given string.
if (strpos($the_string, $the_character) != false) {
echo 'The character "'.$the_character.'" exists in given string.';
} else {
echo 'The character "'.$the_character.'" does not exist in given string.';
}
Another thing to keep in mind is that while you might be looking for an exact word match like “am”, the function will also return TRUE if the string contains words like “sam”, “pam” or “amazing”. This may or may not be what you want to achieve. I just wanted to point it out so that you can proceed with caution.
You can also use > -1
instead of !==
because even if strpos()
returns 0 as index value, it will still be greater than -1. However, remember that the greater than operator (>
) is slower than the strict inequality operator (!==
).
Using stripos() to Check if String Contains Substring [Case Insensitive]
If you are looking for a word or substring inside another string but don’t care about the case of the matched substring, you can use the stripos() function in PHP. This function is similar to the strpos()
function we discussed in the previous section. The only difference is that it ignores the case when looking for a substring inside another string.
PHP
$the_string = "Ben likes both apples and oranges.";
$the_word = "ben";
$the_character = "I";
$the_substring = "LiKes BoTH";
// Output — The word "ben" exists in given string.
if (stripos($the_string, $the_word) !== false) {
echo 'The word "'.$the_word.'" exists in given string.';
}
// Output — The character "I" exists in given string.
if (stripos($the_string, $the_character) !== false) {
echo 'The character "'.$the_character.'" exists in given string.';
}
// Output — The substring "LiKes BoTH" exists in given string.
if (stripos($the_string, $the_substring) !== false) {
echo 'The substring "'.$the_substring.'" exists in given string.';
}
The strpos()
function would have returned FALSE in all the cases above but stripos()
ignored the case and returned TRUE.
Another way to check for a case-insensitive match would be to first convert all the strings and substrings that you want to compare to the same case using either strtolower() or strtoupper() function. After that you could just use the strpos()
function to perform the check. However, it is simply easier to use the stripos()
function.
Using Regex to Check if String Contains Substring
Another option to see if there is a word, character or substring in a string is to use regular expressions. They are generally better suited when you are looking for more complex search patterns in a string.
In situations where you are searching for a particular word, using regular expression might not be a great idea. Keep in mind that using simple strpos()
is about 3 times faster than using regular expressions. Anyway, the following code snippet will show you how to look for a word, character or substring in a string using regular expressions:
PHP
$the_string = "I am 5 years older than you.";
$the_word = "years";
$the_character = "I";
$the_substring = "5 years";
// Output — The word "years" exists in given string.
if (preg_match('/years/', $the_string)) {
echo 'The word "'.$the_word.'" exists in given string.';
}
// Output — The character "I" exists in given string.
if (preg_match('/I/', $the_string)) {
echo 'The character "'.$the_character.'" exists in given string.';
}
// Output — The substring "5 years" exists in given string.
if (preg_match('/5 years/', $the_string)) {
echo 'The substring "'.$the_substring.'" exists in given string.';
}
As I mentioned earlier, using preg_match() only makes sense when you searching for more complicated patterns instead of simple words or substrings. For instance, you can use this function to check if a substring contains any words with 10 or more characters etc. Here is a basic example:
PHP
$the_string = 'Photosynthesis and jeopardize are big words.';
// Output — The given string contains words with 10 or more letters.
if (preg_match('/\w{10,}/i', $the_string)) {
echo 'The given string contains words with 10 or more letters.';
}
You can make all the substring checks which use regex, case-insensitive by adding the flag i
at the end of the pattern. The following example will make it more clear:
PHP
$the_string = "Ben likes both apples and oranges.";
$the_word = "ben";
$the_character = "I";
$the_substring = "LiKes BoTH";
// Output — The word "years" exists in given string.
if (preg_match('/ben/i', $the_string)) {
echo 'The word "'.$the_word.'" exists in given string.';
}
// Output — The character "I" exists in given string.
if (preg_match('/I/i', $the_string)) {
echo 'The character "'.$the_character.'" exists in given string.';
}
// Output — The substring "5 years" exists in given string.
if (preg_match('/LiKes BoTH/i', $the_string)) {
echo 'The substring "'.$the_substring.'" exists in given string.';
}
Using Regex to Check for Exact Word Matches in String
While using strpos()
and stripos()
to look for a word is definitely faster than using regular expressions, using them when you are looking for exact word matches can be problematic. For instance, if you are looking for an exact match of the word “synthesis” in a sentence but it contains the word “Photosynthesis”, you will get a FALSE positive with strpos()
and stripos()
.
Using regular expressions can be very handy in such situations. You can use the expression \b
in a regex pattern to indicate a word boundary. If the word you are looking for is surrounded by two \b
in a regex pattern, the preg_match()
function will only match full words and return FALSE for partial matches. Here is an example:
PHP
$the_string = 'Photosynthesis and jeopardize are big words.';
$the_word = 'synthesis';
// Output — The word "synthesis" has an exact match in given string. [FALSE positive]
if (preg_match('/synthesis/', $the_string)) {
echo 'The word "synthesis" has an exact match in given string. [FALSE positive]';
}
// Output — The word "synthesis" has an exact match in given string. [FALSE positive]
if (strpos($the_string, $the_word)) {
echo 'The word "synthesis" has an exact match in given string. [FALSE positive]';
}
// Output — The word "synthesis" does not have an exact match in given string. [Expected Result]
if (preg_match('/\bsynthesis\b/', $the_string)) {
echo 'The word "synthesis" has an exact match in given string. [FALSE positive]';
} else {
echo 'The word "synthesis" does not have an exact match in given string. [Expected Result]';
}
Using strstr() to Check if String Contains Substring
The PHP strstr() function can also be used to see if a word, character or substring exists within another string. This function returns the portion of the first string starting from and including the first occurrence of the word or substring that we are looking for to the end of haystack. It will return FALSE if needle is not found. This means that we can simply check if a string contains a substring by looking for the return value of strstr()
. Here is an example:
PHP
$the_string = "I am 5 years older than you.";
$the_word = "years";
$the_character = "I";
$the_substring = "5 years";
// Output — The word "years" exists in given string.
if (strstr($the_string, $the_word) !== false) {
echo 'The word "'.$the_word.'" exists in given string.';
}
// Output — The character "I" exists in given string.
if (strstr($the_string, $the_character) !== false) {
echo 'The character "'.$the_character.'" exists in given string.';
}
// Output — The substring "5 years" exists in given string.
if (strstr($the_string, $the_substring) !== false) {
echo 'The substring "'.$the_substring.'" exists in given string.';
}
Just like the strpos()
function, the strstr()
function also contains a case-insensitive counterpart. You can use the stristr()
function to look for a word, character or substring inside another string.
Quick Summary
Let’s recap everything we have covered in this tutorial.
The best method to check if a string contains another word, character or substring is to use the PHP strpos() function. If you intend to perform a case insensitive search, you can use the PHP stripos() function instead. Both these function are easy to use and faster than other methods.
If you are not looking for a particular substring but for a matching pattern, using regular expressions for the search might be a good idea. You can also use regular expressions for simple searches but they are slower than the
strpos()
function.Regular expression can be helpful when you are looking for an exact match of a word. For example, if you are looking for the word “shy”,
strpos()
will also return TRUE if the string contains “Pushy”. On the other hand, you can use the word boundary expression\b
inside thepreg_match()
function to only return FALSE if you are looking for “shy” and the string contains “Pushy”.You can use other available PHP functions like strstr() for case-sensitive matches and stristr() for case-insensitive matches if you don’t want to deal with TRUTHY and FALSEY values.
Let me know if there is anything that you would like me to clarify in this tutorial. Also, you are more than welcome to comment if you know other techniques to check if a string contains another word, character or substring in PHP.
Rate this post —