Core Function StrStr
From Sputnik Wiki
(Difference between revisions)
(Created page with "<pre> StrStr( <haystack>, <needle>, <before_needle>, <find_last>, <case> ) </pre> === Description === Find the first/last occurrence of a string === Parameters === ==== hayst...") |
(→Example) |
||
Line 47: | Line 47: | ||
// using before param to return the text before rather than after the find | // using before param to return the text before rather than after the find | ||
− | $user = strstr($email, '@', true); | + | $user = strstr($email, '@', true); |
echo "Value: $user\n"; // prints name | echo "Value: $user\n"; // prints name | ||
Revision as of 23:29, 29 May 2014
StrStr( <haystack>, <needle>, <before_needle>, <find_last>, <case> )
Contents |
Description
Find the first/last occurrence of a string
Parameters
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/last occurrence of the needle (excluding the needle).
Default: false
find_last
If TRUE, StrStr() will find the last occurrence of the needle instead of the first.
Default: false
case
If TRUE, StrStr() will ignore case and do the comparison case insensitively.
Default: false
Return Value
Returns the portion of string, or FALSE if needle is not found.
Remarks
None.
Example
// basic example $email = 'name@example.com'; $domain = strstr($email, '@'); echo "Value: $domain\n"; // prints @example.com // using before param to return the text before rather than after the find $user = strstr($email, '@', true); echo "Value: $user\n"; // prints name $string1="google.com"; $newstring=strstr($string1,"."); echo "Value: $newstring\n"; // Prints .COM // using find last param to find the last substring instead of the first $string2="google.com.uk"; $newstring1=strstr($string2,".", false, true); echo "Value: $newstring1\n"; // Prints .COM // using ignore case param $string3 = 'cats AnD dogs'; $newstring2 = strstr($string3, 'and', false, false, true); echo "Value: $newstring2\n"; // prints AnD dogs