How to check if String contains substring in PHP

Check if String contains Substring in PHP

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

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

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

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

Output

The substring “JAVA” exists in a given string.”[email protected]”.

Example 2

If we need to return a string before the haystack, then we should write the code as below

Output

admi

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

Output

Pattern matched

Example 2

Output

Array
(
[0] => Welcome
[1] => Welcome
)

That’s all about how to check if string contains substring in PHP.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *