Open In App

PHP strstr() Function

The strstr() function is a built-in function in PHP. It searches for the first occurrence of a string inside another string and displays the portion of the latter starting from the first occurrence of the former in the latter (before if specified). This function is case-sensitive.

Syntax : 



strstr( $string, $search, $before )

Parameters : This function accepts three parameters as shown in the above syntax out of which the first two parameters must be supplied and the third one is optional. All of these parameters are described below: 

Return Value : The function returns the rest of the string (from the matching point), or FALSE, if the string to search for is not found.



Examples: 

Input : $string = "Hello world!", $search = "world"
Output : world!

Input : $string = "Geeks for Geeks!", $search = "k"
Output : ks for Geeks!

Below programs illustrate the strstr() function in PHP :

Program 1: In this program, we will display the portion of $string from the first occurrence of $search.  




<?php
echo strstr("Geeks for Geeks!", "k");
?>
  

Output: 

ks for Geeks! 

Program 2: In this program, we will display the portion of $string before the first occurrence of $search. 




<?php
echo strstr("Geeks for Geeks!", "k", true);
?>
  

Output: 

Gee

Program 3: In this program we will pass an integer as $search. 




<?php
  $string = "Geeks";
  echo strstr($string, 101); // 101 is ASCII value of lowercase e
?>
  

Output: 

eeks

Reference
http://php.net/manual/en/function.strstr.php
 


Article Tags :