Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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: 

  • string: The string from which the search string is located.
  • search: The search parameter, a string, or number to locate.
  • before_search: The default value is false. If it is set to true, it returns the part of the string before the first occurrence of the search parameter.

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




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


Output

eeksforGeeks!

Example 2:

PHP




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

  • string: The string from which the search string is located.
  • search: The search parameter, i.e. a string or number to locate.
  • before_search: The default value is false. If it is set to true, it returns the part of the string before the first occurrence of the search. parameter.

Example:

PHP




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



Last Updated : 22 Oct, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads