Four PHP text functions strstr strrchr substr stristr

WBOY
Release: 2016-07-25 09:07:11
Original
978 people have browsed it
  1. //strstr function
  2. $email = 'liruxing1715@sina.com@qq.com';
  3. $domain = strstr($email, '@');
  4. echo "strstr test result :{$domain}
    ";
  5. $domain = strstr($email, '@', true);
  6. echo "strstr Test result: {$domain}
    ";
  7. / *
  8. The test results are:
  9. strstr Test results: @sina.com@qq.com
  10. strstr Test results: liruxing1715
  11. */
  12. ?>
Copy code

Note: If the string you want to find does not exist If found, then return FALSE. 2. stristr 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 subsequent strings.

  1. //strrchr function
  2. $email = 'liruxing1715@sina.com@qq.com';
  3. $domain = strrchr($email, '@');
  4. echo "strrchr test Result: {$domain}
    ";
  5. /*
  6. The test result is:
  7. strrchr Test result: @qq.com
  8. */
  9. ?>
Copy code

Note: If The string to be searched for 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 interception. If $length is empty, then return from the starting position to the end.

  1. //substr function
  2. $email = 'liruxing1715@sina.com@qq.com';
  3. $domain = substr($email, 10);
  4. echo "substr test results: {$domain}
    ";
  5. $domain = substr($email, 10, 5);
  6. echo "substr Test result: {$domain}
    ";
  7. $domain = substr ($email, -5, 5); //The last digit of the string is -1
  8. echo "substr test result: {$domain}
    ";
  9. /*
  10. The test result is:
  11. substr Test result: 15@sina.com@qq.com
  12. substr Test result: 15@si
  13. substr Test result: q.com
  14. */
  15. ?>
Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!