Home  >  Article  >  Backend Development  >  How to intercept the last few digits of a string in php

How to intercept the last few digits of a string in php

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-09-25 15:10:4016352browse

How to intercept the last few digits of a string in php

substr()

substr() function is used to get a part of a string and return a string.

Syntax:

string substr ( string string, int start [, int length] )

Parameter description is as follows:

How to intercept the last few digits of a string in php

Example:

$str_bh='123456789';$abc=substr($str_bh,-4);
echo $abc;

Output:

6789
<?php
echo substr("Hello world!",6,5);
?>

Output:

world
<?php
echo substr(&#39;abcdef&#39;, 1);
?>

Output:

bcdef

Related recommendations: "php Basic Tutorial"

strstr()

Find the first occurrence of a string in another string and return all characters from that position to the end of the string. If not found, return FALSE.

Syntax:

string strstr ( string string, string needle )

Parameter description is as follows:

How to intercept the last few digits of a string in php

Example:

<?php
$email = &#39;user@5idev.com&#39;;
$domain = strstr($email, &#39;@&#39;);
echo $domain;
?>

Output:

@5idev.com

strrchr()

#Find the last occurrence of a string in another string and return from that position to the end of the string All characters, if not found, returns FALSE.

Syntax:

string strrchr ( string string, string needle )

This function behaves the same as the strstr() function. For the meaning of the parameters, please refer to the strstr() function parameter description above.

Example:

<?php
$str="AAA|BBB|CCC";
echo strrchr($str, "|");
?>

Output:

|CCC

The above is the detailed content of How to intercept the last few digits of a string in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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