Computer >> Computer tutorials >  >> Programming >> PHP

stristr() function in PHP


The stristr() function is used to search for the first occurrence of a string inside another string.

Note − The function is case-insensitive

Syntax

stristr(str, search, before_search)

Parameters

  • str − The string to search in

  • search − The string to search for

  • before_search − 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

The stristr() function returns the matched substring.

Example

The following is an example −

<?php
$mystr = 'Tom Hanks!';
if(stristr($mystr, 'Tim') === FALSE) {
   echo 'Tim is not in the string!';
}
?>

Output

Tim is not in the string!

Example

The following is an example −

<?php
echo stristr("Demo text!","TEXT",true);
?>

Output

Demo