Open In App

Difference between stristr() and strstr() functions in PHP

A string is a sequence of characters stored together. It may contain numbers, characters, or special characters. Strings can be searched and modified. Both of these PHP functions strstr() and stristr() are used to search a string inside another string. 

strstr() Method: The strstr() method is used to search the string inside another string in a case-insensitive manner. This function is considered to be binary-safe. 



Syntax:

strstr(string, search, before_search)

Parameters: 



Example 1: The following code snippet indicates the usage of an integer as the search parameter. The ASCII code for ‘a’ is 97, therefore, the value of ‘e’ is equivalent to 101. Therefore, the string after the first occurrence of the character ‘e’ along with this character is returned. 




<?php
$asciich = 101;
  
echo strstr("GeeksforGeeks!", $asciich);
  
?>

Output
eeksforGeeks!

Example 2:




<?php
  
$find = "GeEks";
  
echo("String after the first occurrence : ");
echo strstr("Here is geeks for GeEks!", $find);
echo('</br>');
echo("String before the first occurrence : ");
echo strstr("Here is geeks for GeEks!", $find, true);
  
?>

Output:

String after the first occurrence : GeEks!
String before the first occurrence : Here is geeks for

stristr() Method: The stristr() method is used to search the string inside another string in a case-sensitive manner. This function is considered to be binary-safe. 

Syntax:

stristr(string, search, before_search)

Parameters:

Example:




<?php
  
$find = "GEEKS";
  
echo("String after the first occurrence : ");
echo stristr("Here is geeks for geeks!", $find);
echo('</br>');
echo("String before the first occurrence : ");
echo stristr("Here is geeks for geeks!", $find, true);
  
?>

Output:

String after the first occurrence : geeks for geeks!
String before the first occurrence : Here is

Note: The only difference between strstr() and stristr() methods is that strstr() method is case insensitive and stristr() method is case sensitive.


Article Tags :