Summary of differences between strstr, strrchr, substr, and stristr functions in php

Source: Internet
Author: User
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@bitsCN.com ';
$ 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@bitsCN.com
Strrchr test result @ bitsCN.com

II. differences between strstr and stristr

Strstr is case sensitive.
Stristr is case insensitive.

The code is as follows:


<? Php
$ Email = 'zhangying @ bitsCN.com ';
$ 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 bitsCN.com
Stristr test results Ying@bitsCN.com

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 @ bitsCN.com ';
$ 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 bitsCN.com
Substr test result bitsCN.com

I have understood the several string truncation functions and can save a lot of trouble during development.

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.