我将如何去抓住下面的字符串的最后 7 个字符?
例如:
$dynamicstring = "2490slkj409slk5409els";
$newstring = some_function($dynamicstring);
echo "The new string is: " . $newstring;
这将显示:
The new string is: 5409els

嗯..像这样?
$newstring = substr($dynamicstring, -7);
使用多字节字符代码的更安全的结果,allways 使用 mb_substr 而不是 substr。
$str = 'Ne zaman seni düşünsem';
echo substr( $str, -7 ) . ' <strong>is not equal to</strong> ' .
mb_substr( $str, -7, null, 'UTF-8') ;

这将是更好的检查之前得到的字符串。
$newstring = substr($dynamicstring, -7);
如果字符更大,则 7 返回最后 7 个字符,否则返回提供的字符串。
或者这样做,如果你需要返回消息或错误,如果长度小于 7
$newstring = (strlen($dynamicstring)>=7)?substr($dynamicstring, -7):"message";
substr documentation
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(83条)