These four functions in php are often confusing, so take some notes here.
1.strstr
strstr — Displays the first string found, the string to be found, and subsequent strings. Case sensitive.
Code listing:
[php]
//strstr function
$email = 'liruxing1715@sina.com@qq.com';
$domain = strstr($email, '@');
echo "strstr Test result: {$domain}
";
$domain = strstr($email, '@', true);
echo "strstr Test result: {$domain}
";
/*
The test result is:
strstr test result: @sina.com@qq.com
strstr test results: liruxing1715
*/
//strstr function
$email = 'liruxing1715@sina.com@qq.com';
$domain = strstr($email, '@');
echo "strstr Test result: {$domain}
";
$domain = strstr($email, '@', true);
echo "strstr Test result: {$domain}
";
/*
The test result is:
strstr test result: @sina.com@qq.com
strstr test results: liruxing1715
*/Note: If the string to be searched is not found, then FALSE is returned.
2. strist
stristr — The function is the same as the strstr function, the only difference is that the case is ambiguous.
3. strrchr
strrchr — Displays the last string found, the string to be found, and the following strings.
Code List:
[php]
//strrchr function
$email = 'liruxing1715@sina.com@qq.com';
$domain = strrchr($email, '@');
echo "strrchr test result: {$domain}
";
/*
The test result is:
strrchr test results: @qq.com
*/
//strrchr function
$email = 'liruxing1715@sina.com@qq.com';
$domain = strrchr($email, '@');
echo "strrchr test result: {$domain}
";
/*
The test result is:
strrchr test results: @qq.com
*/Note: If the string to be searched is not found, then FALSE is returned.
4.substr
substr — In a string, truncate characters according to the given length.
Format: string substr ( string $string , int $start [, int $length ] )
Parameter introduction:
$string: the string to be intercepted;
$start: The starting position to be intercepted, starting from 0 by default; if start is a negative number, the returned string will start from the $start character forward from the end of $string; if the length of string is less than or Equal to start, will return FALSE.
$length: The end position of the interception. If the $length is empty, then return from the starting position to the end.
Code List:
[php]
//substr function
$email = 'liruxing1715@sina.com@qq.com';
$domain = substr($email, 10);
echo "substr test result: {$domain}
";
$domain = substr($email, 10, 5);
echo "substr test result: {$domain}
";
$domain = substr($email, -5, 5); //The last digit of the string is -1
echo "substr test result: {$domain}
";
/*
The test result is:
substr test result: 15@sina.com@qq.com
substr test result: 15@si
substr test results: q.com
*/
//substr function
$email = 'liruxing1715@sina.com@qq.com';
$domain = substr($email, 10);
echo "substr test result: {$domain}
";
$domain = substr($email, 10, 5);
echo "substr test result: {$domain}
";
$domain = substr($email, -5, 5); //The last digit of the string is -1
echo "substr test result: {$domain}
";
/*
The test result is:
substr test result: 15@sina.com@qq.com
substr test result: 15@si
substr test results: q.com
*/