本文翻译自:How can I remove 3 characters at the end of a string in php?
How can I remove 3 characters at the end of a string in php? 如何在php中的字符串末尾删除3个字符? "abcabcabc" would become "abcabc"! “abcabcabc”将成为“abcabc”!
#1楼
参考:https://stackoom.com/question/kCOl/如何在php中的字符串末尾删除-个字符
#2楼
<?php echo substr($string, 0, strlen($string) - 3); ?>
#3楼
<?php echo substr("abcabcabc", 0, -3); ?>
#4楼
Just do: 做就是了:
echo substr($string, 0, -3);
You don't need to use a strlen
call, since, as noted in the substr docs : 您不需要使用strlen
调用,因为,如substr docs中所述 :
If length is given and is negative, then that many characters will be omitted from the end of string 如果给定长度并且为负数,那么将从字符串的末尾省略那么多字符