How do I check if a string contains a specific word in PHP?
Consider the following code:
$a = 'How are you?';
if ($a contains 'are')
echo 'true';
What is the correct way to write the statement if ($a contains 'are') in PHP?
PHP doesn’t have a contains keyword or operator like in some other languages. To check if a string contains a specific substring, you need to use built-in string functions. Here are the most common methods:
Contents
- Using strpos() Function
- Using strstr() Function
- Using str_contains() Function (PHP 8.0+)
- Using preg_match() for Pattern Matching
- Complete Working Example
- Best Practices
Using strpos() Function
The most traditional approach is using strpos(), which finds the position of the first occurrence of a substring:
$a = 'How are you?';
if (strpos($a, 'are') !== false) {
echo 'true';
}
Key points:
strpos()returns the position (index) where the substring is found- If the substring is not found, it returns
false - Always use
!== false(not equal AND not identical) to properly check for false !==is important becausestrpos()could return0(meaning found at position 0), and0 == falsewould be true in PHP
Using strstr() Function
strstr() returns the part of the string from the first occurrence of the substring to the end:
$a = 'How are you?';
if (strstr($a, 'are')) {
echo 'true';
}
Key points:
strstr()returns the substring if found, orfalseif not found- You can also use
stristr()for case-insensitive matching - More convenient when you actually need the substring from the match point
Using str_contains() Function (PHP 8.0+)
PHP 8.0 introduced the str_contains() function which is the most straightforward approach:
$a = 'How are you?';
if (str_contains($a, 'are')) {
echo 'true';
}
Key points:
- Returns
trueif the substring is found,falseotherwise - Much more readable and intuitive than
strpos() - Only available in PHP 8.0 and later
- Case-sensitive by default
Using preg_match() for Pattern Matching
For more complex pattern matching or regular expressions:
$a = 'How are you?';
if (preg_match('/are/', $a)) {
echo 'true';
}
Key points:
- Uses PCRE (Perl Compatible Regular Expressions)
- Powerful for complex matching patterns
- Returns
1if pattern found,0if not found, orfalseon error - Useful for word boundaries, case-insensitive matching, etc.
Complete Working Example
Here’s a complete example showing all methods:
$text = 'How are you today?';
$search = 'are';
// Method 1: strpos()
if (strpos($text, $search) !== false) {
echo "Found using strpos()\n";
}
// Method 2: strstr()
if (strstr($text, $search)) {
echo "Found using strstr()\n";
}
// Method 3: str_contains() (PHP 8.0+)
if (function_exists('str_contains') && str_contains($text, $search)) {
echo "Found using str_contains()\n";
}
// Method 4: preg_match()
if (preg_match('/' . preg_quote($search, '/') . '/', $text)) {
echo "Found using preg_match()\n";
}
Best Practices
Which Method to Choose?
- PHP 8.0+: Use
str_contains()for the most readable and straightforward approach - Legacy PHP: Use
strpos() !== falsefor compatibility with older versions - When you need the matched substring: Use
strstr() - Complex patterns: Use
preg_match()
Case-Insensitive Matching
For case-insensitive searches:
// Using stristr()
if (stristr($text, 'ARE')) {
echo 'Found (case-insensitive)';
}
// Using preg_match() with case-insensitive flag
if (preg_match('/are/i', $text)) {
echo 'Found (case-insensitive)';
}
// Using str_contains() with strtolower (PHP 8.0+)
if (str_contains(strtolower($text), strtolower('ARE'))) {
echo 'Found (case-insensitive)';
}
Word Boundary Matching
To check for whole words (not substrings within words):
// Using preg_match() with word boundaries
if (preg_match('/\bare\b/', $text)) {
echo 'Found whole word "are"';
}
Multiple Substrings
To check if a string contains any of multiple substrings:
$words = ['are', 'you', 'hello'];
foreach ($words as $word) {
if (str_contains($text, $word)) {
echo "Found: $word\n";
}
}
The correct way to write your original code would be:
$a = 'How are you?';
if (str_contains($a, 'are')) {
echo 'true';
}
Or for legacy PHP versions:
$a = 'How are you?';
if (strpos($a, 'are') !== false) {
echo 'true';
}
Sources
- PHP str_contains() documentation
- PHP strpos() documentation
- PHP strstr() documentation
- PHP preg_match() documentation
Conclusion
To check if a string contains a specific word in PHP, you have several options depending on your PHP version and requirements:
- PHP 8.0+: Use
str_contains($string, $substring)for the most readable solution - All PHP versions: Use
strpos($string, $substring) !== falsefor maximum compatibility - When you need the matched text: Use
strstr()orstristr() - For complex patterns: Use
preg_match()with regular expressions
Choose the method that best fits your project’s PHP version and specific needs. For modern PHP development, str_contains() is the preferred choice due to its simplicity and readability.