The stristr() function searches for the first occurrence of a string inside another string.
PHP stristr() Function has the following syntax.
stristr(string,search,before_search)
Parameter | Is Required | Description |
---|---|---|
string | Required. | String to search |
search | Required. | String to search for. If it is a number, it will use it as the ASCII value |
before_search | Optional. | A boolean value whose default is "false". If set to "true", it returns the part of the string before the first occurrence of the search parameter. |
Returns the rest of the string (from the matching point), or FALSE, if the string to search for is not found.
This function is case-insensitive. For a case-sensitive search, use strstr() function.
Find the first occurrence of "world" inside "Hello world!", and return the rest of the string:
<?php
echo stristr("Hello world!","WORLD");
?>
The code above generates the following result.
Search a string for the ASCII value of "o", and return the rest of the string:
<?php
echo stristr("Hello world from java2s.com!",111);
?>
The code above generates the following result.
Return the part of the string before the first occurence of "world":
<?php
echo stristr("Hello from java2s.com world!","WORLD",true);
?>
The code above generates the following result.