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

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

爱喝马黛茶的安东尼
Release: 2023-02-24 21:22:02
Original
16428 people have browsed it

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] )
Copy after login

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;
Copy after login

Output:

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

Output:

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

Output:

bcdef
Copy after login

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 )
Copy after login

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;
?>
Copy after login

Output:

@5idev.com
Copy after login

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 )
Copy after login

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, "|");
?>
Copy after login

Output:

|CCC
Copy after login

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!

Related labels:
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