Strstr, STRRCHR, substr, stristr four functions in PHP to summarize the different _php techniques

Source: Internet
Author: User

PHP strstr, STRRCHR, substr, stristr Four functional usage differences:

PHP strstr STRRCHR substr stristr These four string manipulation functions are especially confusing, often substr,strstr, which can basically satisfy the operation of the string.

Here are some of the different functions.

I. The difference between STRSTR and STRCCHR

STRSTR Displays the first found, the string to look for, and the string that follows.
STRRCHR displays the last found, the string to look for, and the string to follow.

Copy Code code as follows:

<?php
$email = ' test@test.com@jb51.net ';
$domain = Strstr ($email, ' @ ');
echo "STRSTR test results $domain <br>";
$domain = STRRCHR ($email, ' @ ');
echo "STRRCHR test results $domain <br>";
?>

The results are as follows:

STRSTR test results @test. com@jb51.net
STRRCHR test results @jb51. Net

Ii. the difference between strstr and STRISTR

Strstr is case sensitive.
STRISTR is not case sensitive.

Copy Code code as follows:

<?php
$email = ' zhangYing@jb51.net ';
$domain = Strstr ($email, ' Y ');
echo "STRSTR test results $domain <br>";
$domain = Stristr ($email, ' Y ');
echo "STRISTR test results $domain <br>";
?>

The results are as follows:

STRSTR Test Results Jb51.net
STRISTR Test Results Ying@jb51.net

Iii. the difference between strstr and substr

STRSR is matched after interception.
The substr is not matched and is intercepted according to the starting position.

Copy Code code as follows:

<?php
$email = ' zhangYing@jb51.net ';
$domain = Strstr ($email, ' Y ');
echo "STRSTR test results $domain <br>";
$domain = substr ($email,-7);
echo "substr test results $domain <br>";
?>

The results are as follows:
STRSTR Test Results Jb51.net
SUBSTR Test Results Jb51.net

Make sense of this several string intercept functions, you can save a lot of things when developing

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.