Four confusing php text functions strstr strrchr substr stristr

Source: Internet
Author: User

In php, these four functions are often confusing. Take notes here.


1. strstr

Strstr-displays the string to be searched and the subsequent string found for the first time. Case sensitive.


Code List:


[Php]
// Strstr Function
$ Email = 'ruxing1715 @ sina.com@qq.com ';
$ Domain = strstr ($ email ,'@');
Echo "strstr Test Result: {$ domain} <br/> ";
$ Domain = strstr ($ email, '@', true );
Echo "strstr Test Result: {$ domain} <br/> ";
/*
Test results:
Strstr Test Result: @ sina.com@qq.com
Strstr Test Result: liruxing1715
*/

// Strstr Function
$ Email = 'ruxing1715 @ sina.com@qq.com ';
$ Domain = strstr ($ email ,'@');
Echo "strstr Test Result: {$ domain} <br/> ";
$ Domain = strstr ($ email, '@', true );
Echo "strstr Test Result: {$ domain} <br/> ";
/*
Test results:
Strstr Test Result: @ sina.com@qq.com
Strstr Test Result: liruxing1715
*/Note: If the string to be searched is not found, FALSE is returned.


2. stristr

The stristr-function is the same as the strstr function. The only difference is that the case sensitivity is unknown.

3. strrchr

Strrchr-display the last found string and the subsequent string.

Code List:


[Php]
// Strrchr Function
$ Email = 'ruxing1715 @ sina.com@qq.com ';
$ Domain = strrchr ($ email ,'@');
Echo "strrchr Test Result: {$ domain} <br/> ";
/*
Test results:
Strrchr Test Result: @ qq.com
*/

// Strrchr Function
$ Email = 'ruxing1715 @ sina.com@qq.com ';
$ Domain = strrchr ($ email ,'@');
Echo "strrchr Test Result: {$ domain} <br/> ";
/*
Test results:
Strrchr Test Result: @ qq.com
*/Note: If the string to be searched is not found, FALSE is returned.


4. substr

Substr-in a string, the string is truncated Based on the given length.

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

Parameter introduction:

$ String: the string to be truncated;

$ Start: the start position to be intercepted. The default value is from 0. If start is a negative number, the returned string starts from the $ start character at the end of $ string; if the string length is less than or equal to start, FALSE is returned.

$ Length: the end position of the truncation. If the value of $ length is null, the return value ranges from the start position to the end.

Code List:


[Php]
// Substr Function
$ Email = 'ruxing1715 @ sina.com@qq.com ';
$ Domain = substr ($ email, 10 );
Echo "substr Test Result: {$ domain} <br/> ";
$ Domain = substr ($ email, 10, 5 );
Echo "substr Test Result: {$ domain} <br/> ";
$ Domain = substr ($ email,-5, 5); // The last character of the string is-1.
Echo "substr Test Result: {$ domain} <br/> ";
/*
Test results:
Substr test results: 15@sina.com @ qq.com
Substr Test Result: 15 @ si
Substr Test Result: q.com
*/

// Substr Function
$ Email = 'ruxing1715 @ sina.com@qq.com ';
$ Domain = substr ($ email, 10 );
Echo "substr Test Result: {$ domain} <br/> ";
$ Domain = substr ($ email, 10, 5 );
Echo "substr Test Result: {$ domain} <br/> ";
$ Domain = substr ($ email,-5, 5); // The last character of the string is-1.
Echo "substr Test Result: {$ domain} <br/> ";
/*
Test results:
Substr test results: 15@sina.com @ qq.com
Substr Test Result: 15 @ si
Substr Test Result: q.com
*/

 

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.