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:
- The strings with single quotes ('...'). PHP strings of this type do not handle special characters inside quotes;
- The Double-quoted strings ("..."). Unlike single-quoted strings, double-quoted strings in PHP can contain special characters;
- The third way to define strings is to use the Heredoc syntax (<<<).
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:
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:
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:
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:
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:
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:
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:
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: