How to check if string contains substring PHP

How to check if string contains substring PHP

Once in a while we need to check whether a string contains substring, some other string or characters or a value. Checking for existence of a string (or substring) inside another string is easier than it might seem. The following article describes how to check for whether a string contains string in PHP, or simply said how to find string within string in PHP.

Check for string inside a string


Let's have one string called $haystack which holds the value of "mother". Then, we assume another string called $needle which holds the value of "other".

What we are doing:

We want the computer to tell us whether the $haystack mother includes the word other.

How to check if PHP string contains another string

Often, we see two approaches for this. One option is to use the strpos PHP function. The following example shows how this is used:

$pos = strpos($haystack,$needle);

if($pos === false) {
 // string needle NOT found in haystack
else {
 // string needle found in haystack

Watch out, checking for substring inside string in PHP can be tricky. Some people do just something like

if ($pos > 0) {}

or perhaps something like

if (strpos($haystack,$needle)) {
// We found a string inside string

and forget that the $needle string can be right at the beginning of the $haystack, at position zero. The

if (strpos($haystack,$needle)) { }

code does not produce the same result as the first set of code shown above and would fail if we were looking for example for "moth" in the word "mother".

Php string contains substring

Another option is to use the strstr() function. A code like the following could be used:

if (strlen(strstr($haystack,$needle))>0) {
// Needle Found

Note, the strstr() function is case-sensitive. For a case-insensitive search, use the stristr() function.

Which "find substring in a string" method is better?

The strpos() function approach is a better way to find out whether a string contains substring PHP because it is much faster and less memory intensive.

Any word of caution when checking for a substring inside string?

If possible, it is a good idea to wrap both $haystack and $needle with the strtolower() function. This function converts your strings into lower case which helps to eliminate problems with case sensitivity.

Do you have more interesting "how tos" like this?

You are welcome to check other pages related to coding:

PHP loop through string
PHP display array content,
What is heap and stack? 
JavaScript document.write(): How to view the HTML?

Also, check the Related Content block in the right-hand side-bar on every page on this web portal.

PHP check if string contains substring does not work!

Something mentioned on this page does not work for you? Feel free to ask a question in our programming discussion forum.


Discuss this article or this topic in our discussion forum:
(The table bellow shows a list of 8 most recent topics posted in our discussion forum. Visit our discussion forum to see more. It is possible the links below are not related to this page, but you can be certain you will find related posts in the discussion forum. You can post one yourself too.)
Email this article to a friend:
2 + 6 - 3 = 
How can I link to this web page?

It is easy, just include the code provided below into your HTML code.

<a href="" title=" How to check if string contains substring PHP" target="_blank">How to check if string contains substring PHP</a>