The PHP STRSTR function is a method for finding a given string or character within a larger string. Once STRSTR runs, it will return a portion of the string — either before or after the string that was being searched for.

STRSTR is frequently used to split apart strings so they can be properly formatted. STRSTR is a case-sensitive function that is most often used to split apart things like emails and proper names.

What is the PHP STRSTR function?

Before you begin, understanding the basics of PHP Functions is essential. The PHP STRSTR function is one of PHP’s methods for manipulating strings, including PHP string length, as it returns an abbreviated version of the string. STRSTR also returns a “false” if the string is not discovered at all—so it can determine whether a string is actually found inside of another string.

This is one of the most common PHP string functions, including PHP STRLEN and PHP CONCAT.

An example of the PHP STRSTR function

STRSTR is a fairly easy to use and fairly straightforward function. The PHP STRSTR function is defined as follows:

strstr(string $haystack, string $needle, bool $before_needle = false): string|false
PHP for Beginners – Become a PHP Master – CMS Project

Last Updated October 2022

Bestseller
  • 336 lectures
  • All Levels
4.4 (22,925)

PHP for Beginners: learn everything you need to become a professional PHP developer with practical exercises & projects. | By Edwin Diaz, Coding Faculty Solutions

Explore Course

This function can perform the following actions:

The $needle/$haystack naming system is intended to make it a little easier to conceptualize what the program is doing. Let’s take a look at the following example:

Hello World written in script language for PHP

Here, we have set the $haystack to “Hello world!” and the $needle to “world.” We set the $before_needle to “true.” It prints out “Hello” because that’s what comes before the “world” text.

Now, let’s take a look at another example:

PHP coded language written on computer screen in software

We’re using the exact same arguments except that we set $before_needle to “false.” So it’s now printing out the last half of the statement.

What STRSTR does is identify where the $needle falls in the $haystack. If $before_needle is true, it prints the rest of the string. If $before_needle is false, it prints the string from where the $needle is discovered; the function returns the rest.

What can you use STRSTR for?

What would you use STRSTR for? STRSTR finds a string inside another string. It can search for the character that you want and return the part of the string that you want. 

Let’s take a look at the following example:

PHP coding languages. echo strstr coded language on a screen

But importantly, it splits the string into “john” and “@gmail.com.” So it would split a username from an email address at the occurrence of a character, the @ sign. 

STRSTR is very versatile because it allows you to split a string where the needle occurs within a haystack, depending on where the occurrence of a string falls within an existing string. This can be a much easier way to manage PHP strings than with the alternative, which is usually regular expressions.

It should be noted that STRSTR always finds the first occurrence of something; you can’t use it to find the last occurrence. It provides a matching point or false. It can find a specific string or just a character in the string.

Top courses in PHP

PHP for Beginners – Become a PHP Master – CMS Project
Edwin Diaz, Coding Faculty Solutions
4.4 (22,925)
Bestseller
PHP with Laravel for beginners – Become a Master in Laravel
Edwin Diaz, Coding Faculty Solutions
4.5 (11,860)
Bestseller
PHP Unit Testing with PHPUnit
Dave Hollingworth
4.6 (1,753)
Bestseller
APIs in PHP: from Basic to Advanced
Dave Hollingworth
4.7 (436)

More PHP Courses

Using the STRSTR Function to validate strings

As mentioned, STRSTR can be used to return a string, or it can be used to return a “false” if the string isn’t discovered:

Picture of PHP coding language. Coded language PHP examples.

In the above example, the letter “A” is not in the text “Hello, world!” Specifically, this could work to validate email addresses. If an “@” sign is not found within the string, then it is not a valid email address.

There are other ways to validate strings, but STRSTR can be a good tactic, especially if you need to validate a longer string. 

STRSTR vs STRPOS function

The STRSTR function will return a string from the matching set. But what if you want to know where the first occurrence of the search occurs? That’s when you want STRPOS. STRPOS doesn’t return the text but rather where the next starts.

PHP in html language code. Script Language example Hello, World

In the above example, we’re searching for the “world” $needle in the “Hello World!” $haystack. The script returns that the “world” $needle starts at position 6 of the string. Recall that the string to search will always be treated like an array. We could then split the string across position 6 if we wanted to split the string into an array that could then be used while formatted.

STRSTR vs STRISTR

PHP STRISTR is used less frequently than STRSTR, but it is effectively the same function. The only difference is that STRISTR is a case-insensitive version of PHP STRSTR. In the above examples, “world” would match, but “WORLD” and “World” would not. With PHP STRISTR, even “WoRlD” would have matched the “Hello, world!” text.

Learning more about PHP STRSTR

Are you ready to learn more about the PHP STRSTR function? PHP is a great language to learn for those who want to do back-end web development or full-stack web development. If you’re trying to learn the basics of PHP, it may be time to take a look at a PHP programming boot camp. You can learn a lot about string-based functions and general PHP programming.


Frequently asked questions

Is PHP STRSTR case-sensitive?

Yes, this function is case-sensitive. But there is a case-insensitive version called stristr(). It is the same function but rendered in a case-insensitive format.

How does STRSTR work in C?

STRSTR works almost the same way in C as it does in PHP, so you should be able to successfully use STRSTR in C if you already know how to use it in PHP.

Does PHP STRSTR modify the string?

It does not. The original string remains unmodified when you use PHP STRSTR, which is another reason why you can safely use it for validation. You can store the results of PHP STRSTR in another string or in an array if you want to modify it.

Page Last Updated: March 2022