Wednesday, May 8, 2024
HomeLanguagesPhpDifference between stristr() and strstr() functions in PHP

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: 

  • 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("neveropen!", $asciich);
  
?>


Output

eeksforGeeks!

Example 2:

PHP




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


Output:

String after the first occurrence : GeEks!
String before the first occurrence : Here is neveropen 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 neveropen for neveropen!", $find);
echo('</br>');
echo("String before the first occurrence : ");
echo stristr("Here is neveropen for neveropen!", $find, true);
  
?>


Output:

String after the first occurrence : neveropen for neveropen!
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.

Nicole Veronica Rubhabha
Nicole Veronica Rubhabha
A highly competent and organized individual DotNet developer with a track record of architecting and developing web client-server applications. Recognized as a personable, dedicated performer who demonstrates innovation, communication, and teamwork to ensure quality and timely project completion. Expertise in C#, ASP.Net, MVC, LINQ, EF 6, Web Services, SQL Server, MySql, Web development,
RELATED ARTICLES

1 COMMENT

  1. It’s a pity you don’t have a donate button! I’d without
    a doubt donate to this fantastic blog! I suppose for now i’ll
    settle for book-marking and adding your RSS feed to my Google account.
    I look forward to brand new updates and will share this
    site with my Facebook group. Talk soon!

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments