NeuroAgent

How to Check String Contains Word in PHP

Learn multiple methods to check if a string contains a specific word in PHP. Discover strpos(), strstr(), str_contains(), and preg_match() with examples and best practices.

Question

How do I check if a string contains a specific word in PHP?

Consider the following code:

php
$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?

NeuroAgent

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

The most traditional approach is using strpos(), which finds the position of the first occurrence of a substring:

php
$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 because strpos() could return 0 (meaning found at position 0), and 0 == false would be true in PHP

Using strstr() Function

strstr() returns the part of the string from the first occurrence of the substring to the end:

php
$a = 'How are you?';

if (strstr($a, 'are')) {
    echo 'true';
}

Key points:

  • strstr() returns the substring if found, or false if 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:

php
$a = 'How are you?';

if (str_contains($a, 'are')) {
    echo 'true';
}

Key points:

  • Returns true if the substring is found, false otherwise
  • 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:

php
$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 1 if pattern found, 0 if not found, or false on error
  • Useful for word boundaries, case-insensitive matching, etc.

Complete Working Example

Here’s a complete example showing all methods:

php
$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?

  1. PHP 8.0+: Use str_contains() for the most readable and straightforward approach
  2. Legacy PHP: Use strpos() !== false for compatibility with older versions
  3. When you need the matched substring: Use strstr()
  4. Complex patterns: Use preg_match()

Case-Insensitive Matching

For case-insensitive searches:

php
// 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):

php
// 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:

php
$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:

php
$a = 'How are you?';

if (str_contains($a, 'are')) {
    echo 'true';
}

Or for legacy PHP versions:

php
$a = 'How are you?';

if (strpos($a, 'are') !== false) {
    echo 'true';
}

Sources

  1. PHP str_contains() documentation
  2. PHP strpos() documentation
  3. PHP strstr() documentation
  4. 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) !== false for maximum compatibility
  • When you need the matched text: Use strstr() or stristr()
  • 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.