strstr()

Description

This function searches for one string inside of another string. If there is a match, it returns the string beginning from where the matched string is found to the end of the source string. If no match is found the function returns false.

Syntax


mixed strstr( string $source, mixed search [,bool fromStart = false] )

Parameters

$source
The string in which to search for $search.
$search
If the argument is a string, it is used as the string to search for within $source. If it is not a string, the argument will be converted to an integer and used as the ordinal value of the character to search for within $source.
$fromStart
The $fromStart parameter is passed as one of the following values depending up whether the string after (and including) the matched string or the string before the matched string is needed.

  • false – Return the string from the matched string to end of the $source string (including the matched string).
  • true – Return the string from beginning of the beginngin of the source string up to matched string (but not including the matched string).

Return Value

string
The function returns one of the following values if the $search string is found within the $source string:

  • Returns the string from the matched substring to end of source string if $fromstart = false.
  • Returns the string from beginning of source string up to (but not including) the matched substring if $fromstart = true.
bool
false If the $search string is not found in the $source string.

Examples

1. Search for Substrings

This example searches for the string “the” inside the string “We the people, …”. The first call returns a string from the matched string to the end of the source string (including the match). The second call returns a string from the beginning of the source string up to (but not including) the matched string.

$str = "We the people, ...";

echo 'strstr( $str, "the" )       : ' . strstr( $str, "the" )       . "\n";
echo 'strstr( $str, "the", true ) : ' . strstr( $str, "the", true ) . "\n";
?>

The output is shown below. Since the first call didn’t specify the optional third argument, the function used the default value of false. This returns the string from “the” to the end of the source string. The second example specifies the third argument as true causing the function to return the string from the beginning of the source string up, but not including, “the”.

strstr( $str, "the" )       : the people, ...
strstr( $str, "the", true ) : We

2. Testing for Substring

In this example strstr() is used to test for the occurrence of one string inside another. In the first call, the string exists and in the second it doesn’t.

<?php
$str = "We the people, ...";

echo ( strstr( $str, "peo" ) ? "Found" : "Not found" ) . "\n";
echo ( strstr( $str, "xyz" ) ? "Found" : "Not found" ) . "\n";
?>

The output below shows that the first call returns a non-empty string, which is considered true when evaluated as a Boolean expression. The second call returns false.

Found
Not found

3. Search for ASCII Character

This example passes an integer to the $search parameter causing strstr() to search for the ASCII character that corresponds to that value. Here, ASCII 112 corresponds to the lowercase “p”.

<?php
$str = "We the people, ...";

echo 'strstr( $str, 112 )       : ' . strstr( $str, 112 )       . "\n";
echo 'strstr( $str, 112, true ) : ' . strstr( $str, 112, true ) . "\n";
?>

In the output, the ASCII character of decimal 112 (“p”) is found and strstr() returns everything after (and including) the “p” in the first call and everything before the “p” in the second call.

strstr( $str, 112 )       : people, ...
strstr( $str, 112, true ) : We the 

See Also

References