- Php check if string is contained in string
- Checking if a String Contains a Substring in PHP
- What is a string in PHP?
- Checking if a string contains a substring using PHP built-in functions
- PHP String Contains a Substring Examples
- Checking a PHP String for a Substring with Regular Expressions
- See also
- How to Check if a String Contains Another Substring in PHP
- Using New Functions in PHP 8
- Using strpos() and stripos()
Php check if string is contained in string
- Different ways to write a PHP code
- How to write comments in PHP ?
- Introduction to Codeignitor (PHP)
- How to echo HTML in PHP ?
- Error handling in PHP
- How to show All Errors in PHP ?
- How to Start and Stop a Timer in PHP ?
- How to create default function parameter in PHP?
- How to check if mod_rewrite is enabled in PHP ?
- Web Scraping in PHP Using Simple HTML DOM Parser
- How to pass form variables from one page to other page in PHP ?
- How to display logged in user information in PHP ?
- How to find out where a function is defined using PHP ?
- How to Get $_POST from multiple check-boxes ?
- How to Secure hash and salt for PHP passwords ?
- Program to Insert new item in array on any position in PHP
- PHP append one array to another
- How to delete an Element From an Array in PHP ?
- How to print all the values of an array in PHP ?
- How to perform Array Delete by Value Not Key in PHP ?
- Removing Array Element and Re-Indexing in PHP
- How to count all array elements in PHP ?
- How to insert an item at the beginning of an array in PHP ?
- PHP Check if two arrays contain same elements
- Merge two arrays keeping original keys in PHP
- PHP program to find the maximum and the minimum in array
- How to check a key exists in an array in PHP ?
- PHP | Second most frequent element in an array
- Sort array of objects by object fields in PHP
- PHP | Sort array of strings in natural and standard orders
- How to pass PHP Variables by reference ?
- How to format Phone Numbers in PHP ?
- How to use php serialize() and unserialize() Function
- Implementing callback in PHP
- PHP | Merging two or more arrays using array_merge()
- PHP program to print an arithmetic progression series using inbuilt functions
- How to prevent SQL Injection in PHP ?
- How to extract the user name from the email ID using PHP ?
- How to count rows in MySQL table in PHP ?
- How to parse a CSV File in PHP ?
- How to generate simple random password from a given string using PHP ?
- How to upload images in MySQL using PHP PDO ?
- How to check foreach Loop Key Value in PHP ?
- How to properly Format a Number With Leading Zeros in PHP ?
- How to get a File Extension in PHP ?
- How to get the current Date and Time in PHP ?
- PHP program to change date format
- How to convert DateTime to String using PHP ?
- How to get Time Difference in Minutes in PHP ?
- Return all dates between two dates in an array in PHP
- Sort an array of dates in PHP
- How to get the time of the last modification of the current page in PHP?
- How to convert a Date into Timestamp using PHP ?
- How to add 24 hours to a unix timestamp in php?
- Sort a multidimensional array by date element in PHP
- Convert timestamp to readable date/time in PHP
- PHP | Number of week days between two dates
- PHP | Converting string to Date and DateTime
- How to get last day of a month from date in PHP ?
- PHP | Change strings in an array to uppercase
- How to convert first character of all the words uppercase using PHP ?
- How to get the last character of a string in PHP ?
- How to convert uppercase string to lowercase using PHP ?
- How to extract Numbers From a String in PHP ?
- How to replace String in PHP ?
- How to Encrypt and Decrypt a PHP String ?
- How to display string values within a table using PHP ?
- How to write Multi-Line Strings in PHP ?
- How to check if a String Contains a Substring in PHP ?
- How to append a string in PHP ?
- How to remove white spaces only beginning/end of a string using PHP ?
- How to Remove Special Character from String in PHP ?
- How to create a string by joining the array elements using PHP ?
- How to prepend a string in PHP ?
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.
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.
- The strings with single quotes (‘. ‘). PHP strings of this type do not handle special characters inside quotes;
$str = 'An example of a PHP string in single quotes.'
$str = "An example of a PHP string in double quotes.\r\n"
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:
strpos(string, substring, start) stripos(string, substring, start)
- 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:
str_contains(string, substring)
- 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:
/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:
\b/i", $str)) < echo 'true'; >else < echo 'false'; >?> #output: false
See also
How to Check if a String Contains Another Substring in PHP
Monty Shokeen Last updated Feb 26, 2021
A lot of times when I am working with strings in PHP, it becomes important to check if a string contains another substring. PHP has a lot of functions to help you manipulate strings any way you like. We will be using some of these functions today to learn how to check if a string contains a specific substring.
Using New Functions in PHP 8
Three new functions have been added in PHP 8 to help us figure out if a string contains another substring. Please keep in mind that all these functions are case sensitive, and checking for the existence of an empty substring with them will always return true .
- str_contains() checks if a string contains a specific substring.
- str_ends_with() checks if a string ends with a specific substring.
- str_starts_with() checks if a string starts with a specific substring.
$sentence = "Dolphins and Elephants are intelligent animals.";
if(str_contains($sentence, "Elephants"))
echo 'Elephants are intelligent.';
// Elephants are intelligent.
if(str_starts_with($sentence, "Dolphins"))
echo 'Dolphins are intelligent.';
echo 'There is a full stop at the end of the sentence.';
// There is a full stop at the end of the sentence.
As I mentioned earlier, these functions perform a case-sensitive match. You can also perform a case-insensitive match with them by first converting both the string and substring to the same case using strtolower() or strtoupper() .
These functions are ideal if you don’t want to get any extra information about the substrings, like their position. People who need extra information about the match should consider using the functions below.
Using strpos() and stripos()
You can use the PHP strpos() function to get the position of the first occurrence of a substring in a string. The function will return false if it cannot find the substring.
When you use strpos() to check for a substring, make sure that you use the strict inequality operator. This is because the position returned by strpos() starts with 0, and 0 can also equate to false . Using a regular equality check will give you false negatives if the substring is right at the beginning of the main string.
Here are some examples of strpos() :