How to check if a String Contains a Substring in PHP ?
Last Updated :
08 Aug, 2024
A string is a collection of given characters and a substring is a string present in a given string.
In this article, we are going to check if the given string contains a substring by using the PHP strpos() function.
Syntax:
strpos($original_string,$sub_string);
Parameters:
- original_string : The input string
- sub_string : The substring searched in the original input string.
Return type: Boolean value
- True, If substring found
- False, If substring not found.
Example:
PHP
<?php
//input string
$input_string = "sravan kumar author at geeks for geeks ";
//sub string to be checked
$sub = "geeks";
// Test if string contains the word
if (strpos($input_string, $sub) !== false)
{
echo "True";
}
else
{
echo "False";
}
?>
Output:
True
Example 2:
PHP
<?php
//input string
$input_string = "sravan kumar author at geeks for geeks ";
//sub string to be checked
$sub = "computer";
// Test if string contains the word
if (strpos($input_string, $sub) !== false)
{
echo "True";
}
else
{
echo "False";
}
?>
Output:
False
Example 3: The following example also considers space.
PHP
<?php
//input string
$input_string = "geeks for geeks java python php";
//sub string to be checked
$sub = " ";
// Test if string contains the word
if (strpos($input_string, $sub) !== false)
{
echo "True";
}
else
{
echo "False";
}
?>
Output:
True
Example 4:
PHP
<?php
//input string
$input_string = "geeks for geeks java python php";
//sub string to be checked
$sub = " dbms";
// Test if string contains the word
if (strpos($input_string, $sub) !== false)
{
echo "True";
}
else
{
echo "False";
}
?>
Output:
False
Using substr_count function
The substr_count function in PHP efficiently counts the occurrences of a substring within a string. It returns the total count of occurrences, allowing for simple checks of substring presence or frequency within the given string.
Example:
PHP
<?php
$string = "Hello World";
$substring = "World";
$count = substr_count($string, $substring);
if ($count > 0) {
echo "Substring found";
} else {
echo "Substring not found";
}
?>
Using preg_match Function
The preg_match function in PHP is used to perform a regular expression match. It returns the number of times the pattern matches (which will be 1 if it matches and 0 if it doesn’t).
Example:
PHP
<?php
//input string
$input_string = "sravan kumar author at geeks for geeks ";
//pattern to be checked
$pattern = "/geeks/";
// Test if string contains the pattern
if (preg_match($pattern, $input_string)) {
echo "True";
} else {
echo "False";
}
?>
Using str_contains() Function
The str_contains() function checks if a substring is present in a string. It returns a boolean value: true if the substring is found, and false if it is not.
Example: The PHP code you provided is almost correct, but it uses the str_contains function which is only available in PHP 8.0 and later.
PHP
<?php
//input string
$input_string = "sravan kumar author at geeks for geeks ";
//sub string to be checked
$sub = "geeks";
// Test if string contains the word
if (str_contains($input_string, $sub)) {
echo "True";
} else {
echo "False";
}
?>
Output:
True