Table of Contents
Introduction
A string is a sequence of characters used as a literal constant or variable. The specific part of a string is known as a substring. In this article, we will see different ways to check if string contains substring in PHP.
How to check if a string contains substring in PHP?
1) strpos() function:
PHP provides a strpos()
function to check if a string contains a specific substring or not. This function can return the position of the first occurrence of a substring in a string. If unable to find a substring, then it returns false.
Syntax
int strpos( $string, $substring )
This function can accept two parameters: $string
specifies a text where searching performs, and $substring
specifies a substring, which you have to search.
Example 1
1 2 3 4 5 6 7 8 9 10 |
<?php $str1 = "Welcome to Java2Blog"; $substr1 = "Java2Blog"; if(strpos($str1, $substr1) !== false) { echo "true"; } ?> |
Output
true
Code Explanation
The above program evaluates true because the main string $str1
contains a substring Java2Blog
. Hence it prints out true.
Example 2
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php $str = "Welcome to Java2Blog"; $word = "Java2Blog"; if(strpos($str, $word) === 0) { echo "The word ".$word." found at the beginning"; } else{ echo "The word ".$word." not found at the beginning"; } ?> |
Output
The word Java2Blog not found at the beginning.
Code Explanation
The above program evaluates "The word Java2Blog not found at the beginning" as an output because the main string $str contains a substring "Java2Blog" is not at the beginning.
2) strstr() function:
strstr()
function is also a PHP function to see if the substring, character, or word can exist within another string. This function returns a portion of the first-string starting from and including the first occurrence of word or substring that we are looking for to the end of the input string.
This function returns false if a substring does not exist in input string, which means that we can check if a string contains a substring by looking for the return value of strstr()
.
Syntax
strstr(string $haystack, mixed $needle [, bool $before_needle = FALSE]) : string
In the above syntax, the parameters are described as below:
haystack
: It is an input string.
needle
: If the needle is not a string, it is transformed to an integer and used as an ordinal value of a character.
before_needle
: If true, the strstr() function can return a part of the haystack before the first occurrence of the needle (excluding the needle). By default, it is false.
Example
1 2 3 4 5 6 7 8 9 10 |
<?php $mainstring = "Welcome to PHP2Blog"; $substring = "Welcome"; if (strstr($mainstring, $substring) !== false) { echo 'The substring "'.$substring.'" exists in a given string.'; } ?> |
Output
The substring “Welcome” exists in a given string.
3) stristr() function:
Both stristr()
and strstr()
functions are used to find out the first occurrence of the string, but the difference is that the stristr() function is case insensitive while the strstr() function is case sensitive.
Syntax
stristr(string $haystack, mixed $needle [, bool $before_needle = FALSE]) : string
In the above syntax, the parameters are described as below:
haystack
: It is an input string.
needle
: If the needle is not a string, it is transformed to an integer and used as an ordinal value of a character.
before_needle
: If true, the strstr() function can return a part of the haystack before the first occurrence of the needle (excluding the needle). By default, it is false.
Example 1
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php <?php $substring = "JAVA"; if (stristr($email_id, $substring) !== false) { echo 'The substring "'.$substring.'" exists in a given string."'.$email_id.'".'; } ?> |
Output
Example 2
If we need to return a string before the haystack
, then we should write the code as below
1 2 3 4 5 6 7 8 |
<?php $domain_name = stristr($email_id, 'N', TRUE); echo $domain_name; ?> |
Output
admi
Further reading:
4) preg_match() function:
preg_match()
function used to implement a regular expression match. The preg_match() function can return several matches of a pattern.
Its value is 0 times if no match occurs or one time because it stops searching after the first match. This function can return false if an error occurs.
Syntax
int preg_match(string $pattern, string $subject [, array & $matches [, int $flags = 0 [, int $offset = 0 ]]])
Example 1
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php $str = 'Welcome to Java2Blog'; $result = preg_match('/(Welcome)/', $str); if($result == 1) { echo "Pattern matched"; } else { echo "Do not match!!!"; } ?> |
Output
Pattern matched
Example 2
1 2 3 4 5 6 7 8 9 |
<?php $str = 'Welcome to Java2Blog'; preg_match('/(Welcome)/', $str, $matches); print_r($matches); ?> |
Output
Array
(
[0] => Welcome
[1] => Welcome
)
That’s all about how to check if string contains substring in PHP.