Introduction
When working with strings in PHP, you might often need to determine whether a specific word exists within a string. This can be particularly useful for tasks such as text analysis, data validation, or search functionality. PHP provides several methods to achieve this, each suitable for different scenarios and versions of the language.
Checking String Containment
Using str_contains()
in PHP 8+
With the introduction of PHP 8.0.0, the str_contains()
function was added to simplify checking if a string contains a specific substring or word. This function is straightforward and easy to use:
$haystack = 'How are you?';
$needle = 'are';
if (str_contains($haystack, $needle)) {
echo 'true';
} else {
echo 'false';
}
Key Points:
str_contains()
is case-sensitive.- Ensure the
$needle
is not empty to avoid unintended results.
Before PHP 8
For versions prior to PHP 8.0.0, you can use the strpos()
function. This function finds the position of the first occurrence of a substring within another string:
$haystack = 'How are you?';
$needle = 'are';
if (strpos($haystack, $needle) !== false) {
echo 'true';
}
Key Points:
- Use
!== false
to correctly handle cases where the needle is at the start of the haystack (strpos()
returns 0). strpos()
is also case-sensitive.
Using Regular Expressions for Word Matching
Regular expressions offer a powerful way to match whole words, avoiding partial matches within other words. The \b
(word boundary) assertion helps in this regard:
function containsWord($str, $word)
{
return !!preg_match('#\b' . preg_quote($word, '#') . '\b#i', $str);
}
$a = 'How are you?';
if (containsWord($a, 'are')) {
echo 'true';
} else {
echo 'false';
}
Key Points:
\b
matches positions where one side is a word character and the other is not.preg_quote()
ensures special characters in$word
are treated literally.- The
i
modifier makes the match case-insensitive.
Considerations for Non-ASCII Characters
Regular expressions with \b
might not work correctly with non-English languages. For Unicode support, you can define custom word boundaries:
function contains($str, $word) {
return preg_match('/(?<=\\s|.|:;,"|^)' . preg_quote($word, '/') . '(?=\\s|.|:;,",|$)/u', $str);
}
$example = '¿Cómo estás?';
if (contains($example, 'estás')) {
echo 'true';
} else {
echo 'false';
}
Key Points:
- Use
(?<=...)
and(?=...)
for more flexible word boundaries. - The
/u
modifier enables Unicode support in regular expressions.
Conclusion
Choosing the right method depends on your PHP version and specific requirements. For straightforward substring checks, str_contains()
is ideal if you’re using PHP 8 or later. Otherwise, strpos()
remains a reliable option. For precise word matching, especially when dealing with complex text, regular expressions provide robust solutions.
Best Practices
- Always check for empty strings to avoid unexpected results.
- Consider case sensitivity and language specifics when choosing your method.
- Regularly test edge cases to ensure reliability in different scenarios.