php 如何在php中删除字符串末尾的3个字符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4915753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I remove 3 characters at the end of a string in php?
提问by bensiu
How can I remove 3 characters at the end of a string in php? "abcabcabc" would become "abcabc"!
如何在php中删除字符串末尾的3个字符?“abcabcabc”将变成“abcabc”!
回答by bensiu
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 文档中所述:
If length is given and is negative, then that many characters will be omitted from the end of string
如果给定长度并且为负数,则字符串末尾将省略许多字符
回答by KomarSerjio
<?php echo substr("abcabcabc", 0, -3); ?>
<?php echo substr("abcabcabc", 0, -3); ?>
回答by Jan
<?php echo substr($string, 0, strlen($string) - 3); ?>