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
*/