PHP strstr() Function
PHP strstr() Function
The strstr() function in PHP finds the first occurrence of a string. It returns part of the parameter haystack string starting from and including the first occurrence of needle to the end of the haystack parameter. This function is case-sensitive.
Syntax
strstr ( string $haystack , mixed $needle [, bool $before_needle] )
Parameter
haystack(required)- This parameter signifies the string to be unescaped.
needle(required)- This parameter represents a string of one or more characters. If it is not a string, it is converted to an integer and applied as the ordinal value of a character.
before_needle(optional)- This parameter accepts Boolean value, and its default value is False. If set TRUE, it returns the part of the haystack before the first occurrence of the needle parameter.
Return
This function returns the portion of the string, or FALSE if the needle is not found.
Example 1
Output
String: Hello World! Hello my PHP! After the strstr() function... The new string: my PHP!
Example 2
Output
String: Hello World! Hello my PHP! After the strstr() function... The new string: World! Hello my PHP!
Example 3
Output
String: Hello World! Hello my PHP! After the strstr() function... The new string: Hello
Example 4
<?php $email = '[email protected]'; echo("The email: ".$email); $domain = strstr($email, '@'); echo "\nThe domain: ".$domain; // prints @example.com echo"\nUser name: ".strstr($email, '@', true); // befor_needle= true ?>
Output
The email: [email protected] The domain: @tutorialandexample.com User name: reema