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 −

 Live Demo

<?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 −

 Live Demo

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

Output

Demo

Updated on: 24-Jun-2020

181 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements