top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is the Difference between strstr and stristr in php?

+1 vote
904 views
What is the Difference between strstr and stristr in php?
posted Jul 17, 2014 by Amritpal Singh

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

0 votes

Both are used to find the first occurrence of the string but stristr( ) is case insensitive.

Syntax
$output = strstr ($string1, $string2)
$output = stristr ($string1, $string2)

where string2 would be searched in the string1 and returns the part of string1 starting from and including the first occurrence of string2 to the end of string1.

Example

<?php
$email = 'salil@queryhomE.com';
$target_str = strstr($email, 'E');
echo $target_str;
$target_str = stristr($email, 'E');
echo $target_str
?>

Output

E.com
eryHome.com
answer Jul 18, 2014 by Salil Agrawal
0 votes

strstr() and stristr both are used to find the first occurence of the string but stristr( ) is case insensitive.

strstr() - Find first occurrence of a string
Syntax:
strstr ($string, string)

Example:

<?php
$email = 'rajesh@tEst.com';
$domain_name = strstr($email, 'E');
echo $domain_name;

?>

Output:Est.com

stristr() - Find first occurrence of a string (Case-insensitive)
Syntax:
stristr ($string, string)

Example:
<?php
$email = 'rajesh@tEst.com';
$domain_name = stristr($email, 'E');
echo $domain_name;

?>
Output: esh@tEst.com

answer Jul 18, 2014 by Rahul Mahajan
...