This article describes the differences between strstr, strrchr, substr, and stristr functions in php. these four functions are commonly used string-related functions in PHP, for more information about how to use strstr, strrchr, substr, and stristr functions in php, see:
The strstr strrchr substr stristr functions in php are especially confusing. substr and strstr are commonly used to perform string operations.
The following describes the differences between these functions.
I. differences between strstr and strcchr
Strstr: the first string to be found, and the subsequent string.
Strrchr shows the last found string and the subsequent string.
The code is as follows:
<? Php
$ Email = 'Test @ test.com@jb51.net ';
$ Domain = strstr ($ email ,'@');
Echo "strstr test result $ domain
";
$ Domain = strrchr ($ email ,'@');
Echo "strrchr test result $ domain
";
?>
The result is as follows:
Strstr test result @ test.com@jb51.net
Strrchr test result @ jb51.net
II. differences between strstr and stristr
Strstr is case sensitive.
Stristr is case insensitive.
The code is as follows:
<? Php
$ Email = 'zhangying @ jb51.net ';
$ Domain = strstr ($ email, 'y ');
Echo "strstr test result $ domain
";
$ Domain = stristr ($ email, 'y ');
Echo "stristr test result $ domain
";
?>
The result is as follows:
Strstr test result jb51.net
Stristr test results Ying@jb51.net
III. differences between strstr and substr
Strsr is intercepted after matching.
Substr does not match. it is intercepted based on the starting position.
The code is as follows:
<? Php
$ Email = 'zhangying @ jb51.net ';
$ Domain = strstr ($ email, 'y ');
Echo "strstr test result $ domain
";
$ Domain = substr ($ email,-7 );
Echo "substr test result $ domain
";
?>
The result is as follows:
Strstr test result jb51.net
Substr test result jb51.net
I have understood the several string truncation functions and can save a lot of trouble during development.