Checking if a String Contains a Substring in PHP

To check if a PHP string contains a substring, you can use the strpos($string, $substring) function. If the string contains the searched substring, the strpos() function will return the index of the first occurrence of the substring in the string; otherwise "false". In PHP, indexes start at 0. As of PHP 8+, you can use the new str_contains($string, $substring) function to check if a string contains the desired substring or word. The str_contains() function returns "true" if the string contains a substring or "false" otherwise. Also, you can use Regular Expressions to check if a PHP string contains a substring. Regular Expressions, unlike other methods, provide more flexibility but are slower. In this PHP String Contains Substring example, we use the strpos() function to check if the string contains a substring. Click Execute to run the PHP String Contains example online and see the result.
Checking if a String Contains a Substring in PHP Execute
<?php

if(strpos("PHP string example.", "PHP") !== false){
    echo "Found!";
}
?>
Updated: Viewed: 18336 times

What is a string in PHP?

PHP strings are sequences of characters, where each character is represented by one byte. Hence PHP supports 256 different characters, and there is no native support for Unicode. String variables in PHP can contain alphanumeric characters. PHP provides a wide set of built-in functions for splitting, comparing, concatenating, replacing, and interpolating strings. You can also convert string to array, convert string to int, and find the length of a string.

To create a string in PHP, you can use:

  1. The strings with single quotes ('...'). PHP strings of this type do not handle special characters inside quotes;

    PHP String Example with Single Quotes
    $str = 'An example of a PHP string in single quotes.'

  2. The Double-quoted strings ("..."). Unlike single-quoted strings, double-quoted strings in PHP can contain special characters;

    PHP String Example with Double Quotes
    $str = "An example of a PHP string in double quotes.\r\n"

  3. The third way to define strings is to use the Heredoc syntax (<<<).

    PHP Heredoc String Example
    <<<MY_STRING
    
    PHP multiline string example.
    
    The heredoc PHP string may include special characters. 
    
    MY_STRING;

Checking if a string contains a substring using PHP built-in functions

You can use the strpos() and stripos() functions to check if a PHP string contains a substring. The strpos() method searches for an occurrence of a substring in a PHP string and returns the index of the first occurrence or "false" otherwise. The strpos() method is case-sensitive. If you want to check if a PHP string contains a substring case-insensitive, you can use the stripos() method or convert the string and substring to lowercase before checking. The following is the syntax of the PHP strpos() and stripos() functions:

PHP strpos() and stripos() Syntax
strpos(string, substring, start)
      
stripos(string, substring, start)

Where:
  • string (required): the string to search in;
  • substring (required): the string to search for;
  • start (optional): position from which to search. If the start is a negative number, the search is performed from the end of the string.

In PHP 8 and higher, you can use str_contains() function to check if a string contains the desired substring. The str_contains() function returns the boolean "true" if the substring is found in the string and "false" otherwise. The str_contains() is case-sensitive. The following is the syntax of the PHP str_contains() function:

PHP str_contains() Syntax
str_contains(string, substring)

Where:
  • string (required): the original string;
  • substring (required): the substring to search for in the string.

PHP String Contains a Substring Examples

The following are examples of checking if a string contains a substring in PHP:

Checking if the PHP string contains a substring using str_contains()

The following is an example of checking if a PHP string contains a substring using the new PHP 8 str_contains() function:

PHP str_contains() Example
<?php
if (str_contains("PHP String Contains Example", "PHP")) {
    echo "Found!";
}
?>

#output: Found!

Checking if the PHP string contains a substring using strpos (case-sensitive)

The following is an example of checking if a substring exists in a PHP string in a case-sensitive way using the strpos() function:

PHP strpos() Example
<?php
$string = "PHP String Contains Example";
$substr = "Contains";

if (strpos($string, $substr) !== false)
{
  echo "true";
}
else
{
  echo "false";
}
?>

#output: true

Checking if the PHP string contains a substring using stripos (case-insensitive)

The following is an example of checking for the presence of a substring in a PHP string case-insensitively using the stripos() function:

PHP stripos() Example
<?php
echo stripos("PHP string example","string");
?>

#output: 4

Checking a PHP String for a Substring with Regular Expressions

The following are examples of checking if a string contains a substring using Regular Expressions in PHP:

Checking if the PHP string contains a substring using preg_match()

To test a string for a substring using regular expressions, you can use the preg_match() function. The following is an example of checking if a substring exists in a PHP string using the preg_match() function:

PHP preg_match() Example
<?php
if (preg_match("/example/i", "PHP string example")) {
  echo "Found!";
} else {
  echo "Not found!";
}
?>

#output: Found!

Using Regular Expressions to check if a string contains a substring, case-insensitively:

To check for a substring in a string case-insensitively using regular expressions, you need to add anchor "i" to the Regular Expression:

PHP preg_match() Case Insensitive Example
<?php
$a = "substring";
$str = "PHP substr example";

if (preg_match("/{$a}/i", $str)) {
  echo 'Found';
} else {
  echo "Not Found!";
}
?>

#output: Not Found!

Using the Regex function to search specific words in a string

To find a specific word in a string using a Regular Expression, you need to add anchor "\b" to the Regular Expression:

PHP preg_match() Example
<?php
$a = "str";
$str = "PHP substr example";

if (preg_match("/\b{$a}\b/i", $str)) {
  echo 'true';
} else {
  echo 'false';
}
?>

#output: false

See also