Oct 16, 2014

What is the functionality of the function strstr and stristr?


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

string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] )

haystack
The input string.
needle
If needle is not a string, it is converted to an integer and applied as the ordinal value of a character.
before_needle
If TRUE, strstr() returns the part of the haystack before the first occurrence of the needle (excluding the needle). By default it is FALSE.
strstr() – Find first occurrence of a string
Syntax:
strstr ($string, string)

Example:

<?php
$email = 'siddharth@yaHoo.com';
$domain_name = strstr($email, 'H');
echo $domain_name;
?>

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

Example:

<?php
$email = 'siddharth@yaHoo.com';
$domain_name = stristr($email, 'H');
echo $domain_name;
?>

Output: harth@yaHoo.com
If we want to return the string before the haystack then we need to write the code as:

<?php
$email = 'siddharth@yaHoo.com';
$domain_name = stristr($email, 'H', TRUE);
echo $domain_name;
?>

Output: sidd

0 comments:

Post a Comment