如何获取PHP字符串的最后7个字符? [英] How can I get the last 7 characters of a PHP string?

查看:299
本文介绍了如何获取PHP字符串的最后7个字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该如何获取下面字符串的最后7个字符?

How would I go about grabbing the last 7 characters of the string below?

例如:

$dynamicstring = "2490slkj409slk5409els";
$newstring = some_function($dynamicstring);
echo "The new string is: " . $newstring;

哪个会显示:

The new string is: 5409els

推荐答案

使用 substr() ,其中第二个参数为负数.

Use substr() with a negative number for the 2nd argument.

$newstring = substr($dynamicstring, -7);

php文档:

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

如果start为负,则返回的字符串将从字符串末尾的第一个字符开始.

这篇关于如何获取PHP字符串的最后7个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆