PHP Tutorial - PHP stristr() Function






Definition

The stristr() function searches for the first occurrence of a string inside another string.

Syntax

PHP stristr() Function has the following syntax.

stristr(string,search,before_search)

Parameter

ParameterIs RequiredDescription
stringRequired.String to search
searchRequired.String to search for. If it is a number, it will use it as the ASCII value
before_searchOptional.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.

Return

Returns the rest of the string (from the matching point), or FALSE, if the string to search for is not found.





Note

This function is case-insensitive. For a case-sensitive search, use strstr() function.

Example

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.

Example 2

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.





Example 3

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.