Easy to confuse four PHP text functions strstr strrchr substr stristr_php Tutorial

Source: Internet
Author: User
These four functions in PHP are often confusing, and here are some notes to take.


1, Strstr

strstr-displays the string that was found for the first time, to find, 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 results are:
STRSTR test Result: @sina. com@qq.com
STRSTR test Result: 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 results are:
STRSTR test Result: @sina. com@qq.com
STRSTR test Result: liruxing1715
*/NOTE: Returns FALSE if the string you are looking for is not found.


2, Stristr

The stristr-function is the same as the STRSTR function, and the only difference is the sense of capitalization.

3, STRRCHR

The strrchr-displays the string that was last found, to find, and subsequent strings.

Code Listing:


[PHP]
STRRCHR function
$email = ' liruxing1715@sina.com@qq.com ';
$domain = STRRCHR ($email, ' @ ');
echo "STRRCHR test result: {$domain}
";
/*
The test results are:
STRRCHR test Result: @qq. com
*/

STRRCHR function
$email = ' liruxing1715@sina.com@qq.com ';
$domain = STRRCHR ($email, ' @ ');
echo "STRRCHR test result: {$domain}
";
/*
The test results are:
STRRCHR test Result: @qq. com
*/NOTE: Returns FALSE if the string you are looking for is not found.


4, substr

substr-in a string, the character is intercepted according to the given length.

Format: String substr (string $string, int $start [, int $length])

Parameter description:

$string: the string to intercept;

$start: The starting position to intercept is starting at 0, and if start is a negative number, the returned string starts at the end of the $string at the beginning of the first $start character, or FALSE if the length of the string is less than or equal to start.

$length: The end position of the intercept, if the first $length is empty, then return from the start position to the end.

Code Listing:


[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 results are:
SUBSTR test Result: 15@sina.com@qq.com
SUBSTR test Result: 15@si
SUBSTR test Result: 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 results are:
SUBSTR test Result: 15@sina.com@qq.com
SUBSTR test Result: 15@si
SUBSTR test Result: q.com
*/

http://www.bkjia.com/PHPjc/477898.html www.bkjia.com true http://www.bkjia.com/PHPjc/477898.html techarticle these four functions in PHP are often confusing, and here are some notes to take. 1. Strstr Strstr shows the first found, the string to find, and the following string. Case sensitive. to be a substitute for ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.