How to Use the PHP *strstr()* Function to Search a String

PHP strstr()

This tutorial will show you how to use the PHP strstr() function to search and return a portion of a string, with examples.

What is the strstr() Function For?

The strstr() function searches a string and returns the matching portion of the string and everything that follows or precedes it. If the string does not contain a match for the search, a value of false is returned.

It’s a hard one to visualize – the examples below will show you precisely what this means.

If you’re simply looking to confirm whether a string contains a substring or value, use the strpo() function instead – it’s faster.

The strstr() function is useful for extracting information from strings – for example, the query string from a URL or the domain from an email address can be extracted by searching them using strstr().

strstr() PHP Function Syntax

Here’s the syntax for the strstr() function:

strstr($NEEDLE, $HAYSTACK, $BEFORE_NEEDLE)

Note that:

  • $NEEDLE is the string you wish to search for
    • This can be of any length
  • $HAYSTACK is the string you wish to search for $NEEDLE
  • $BEFORE_NEEDLE
    • Defaults to FALSE
    • If TRUE, the part of $HAYSTACK returned by strstr() will be everything before the match, rather than after
  • strstr() will return one of the following:
    • A boolean value of FALSE if the string is not found
    • The first occurrence of $NEEDLE and everything following it if $BEFORE_NEEDLE is not set or is FALSE
    • The first occurrence of $NEEDLE and everything preceding it if $BEFORE_NEEDLE is set to TRUE

Examples

Here are some examples of how the strstr() function can be used.

Get Everything Following a Search Term – Extracting the Query String from a URL

Below, the default behavior of strstr() is shown – a string is searched for a substring, and when found, is returned along with everything after it:

$url  = 'https://example.com/mypage.php?page=2&colour=red';
$query = strstr($url, '?');
echo $query; // prints ?page=2&colour=red

Get Everything Preceding a Search Term – Extracting the Username From an Email Address

By setting the third parameter to TRUE, when the search succeeds, it is returned with everything preceding it, rather than everything that follows it:

$email = '[email protected]';
$username = strstr($email, '@', true);
echo $username; // prints fred.fake

Confirming a Whether the Search Succeeded

When the search fails, a boolean value of FALSE is returned:

$email = '[email protected]';
$found = strstr($email, '?');

if($found){
    echo $found;
} else {
    echo 'Not found';// The if statement above will fail in this instance as the searched string does not contain a ?
}

 

SHARE:
nv-author-image

Brad Morton

I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Reply

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