PHP strstr()Functions
Examples
Search whether "world" exists in "Hello world!". If it exists, return the string and the remaining part after it:
echo strstr("Hello world!","world"); //Output: world!
Run Example »
Definition and Usage
The strstr() function searches for a string inside another string. If it exists, it returns the string and the remaining part; otherwise, it returns FALSE.
Note:This function is binary-safe.
Note:This function is case-sensitive. For a case-insensitive search, usestristr()function.
Syntax
strstr(string,search,before_search)
| Parameter | Description |
|---|---|
| string | Required. Specifies the string to be searched. |
| search | Required. Specifies the string to search for. If this parameter is a number, it searches for the character matching the ASCII value of that number. |
| before_search | Optional. A boolean value with a default value of "false". If set to "true", it will returnsearchthe part of the string before the first occurrence of the parameter. |
Technical Details
| Return Value: | Returns the remaining part of the string (from the match point). If the searched string is not found, it returns FALSE. |
|---|---|
| PHP Version: | 4+ |
| Changelog: | In PHP 5.3, thebefore_searchparameter was added. |
More Examples
Example 1
Search the string by the ASCII value of "o" and return the remaining part of the string:
<?php
echo strstr("Hello world!",111);
?>
echo strstr("Hello world!",111);
?>
Run Example »
Example 2
Return the part of the string before the first occurrence of "world":
<?php
echo strstr("Hello world!","world",true);
?>
echo strstr("Hello world!","world",true);
?>
Run Example »
PHP String Reference Manual Other Extensions