Summary of the Differences of strstr strrchr substr and stristr in php

  • 2021-07-18 07:36:06
  • OfStack

Differences in usage of strstr, strrchr, substr and stristr in php:

The four string operation functions strstr strrchr substr stristr in php are particularly confusing, and substr and strstr are commonly used, which can basically meet the operation of strings.

Let's introduce the difference between these functions under 1.

1. The difference between strstr and strcchr

strstr displays the first time you find it, the string you want to find, and the following strings.
strrchr displays the last found, the string to be found, and the following strings.


<?php
$email = 'test@test.com@ofstack.com';
$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@ofstack.com
strrchr Test Results @ ofstack.com

2. Differences between strstr and stristr

strstr is case sensitive.
stristr is case insensitive.


<?php
$email = 'zhangYing@ofstack.com';
$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 ofstack. com
stristr test results Ying@ofstack.com

3. Differences between strstr and substr

strsr is post-match interception.
substr is a mismatch, truncated according to the starting position.


<?php
$email = 'zhangYing@ofstack.com';
$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 ofstack. com
substr test results ofstack. com

This string interception function is understood, which can save a lot of things in development


Related articles: