PHP STRSTR: How to Use the STRSTR Function
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
Last Updated May 2022
PHP for Beginners: learn everything you need to become a professional PHP developer with practical exercises & projects. | By Edwin Diaz, Coding Faculty SolutionsExplore Course
This function can perform the following actions:
- $haystack. The $haystack refers to the text that is being searched.
- $needle. The $needle refers to the text that is being searched for, which can be a single character or a longer string.
- $before_needle. This boolean is optional, but it defines whether the string returned is before or after the needle position.
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:
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:
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:
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
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:
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.
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.