【问题标题】:How can I remove three characters at the end of a string in PHP?如何在 PHP 中删除字符串末尾的三个字符?
【发布时间】:2011-06-22 09:25:48
【问题描述】:

如何在 PHP 中删除字符串末尾的三个字符?

“abcabcabc”会变成“abcabc”!

【问题讨论】:

标签: php string


【解决方案1】:

只要做:

echo substr($string, 0, -3);

您不需要使用strlen 调用,因为如the substr documentation 所述:

如果给定长度并且是负数,那么字符串末尾会省略很多字符

【讨论】:

    【解决方案2】:
    <?php echo substr("abcabcabc", 0, -3); ?>
    

    【讨论】:

      【解决方案3】:
      <?php echo substr($string, 0, strlen($string) - 3); ?>
      

      【讨论】:

      • strlen() 是不必要的。
      猜你喜欢
      • 2020-01-01
      • 2010-11-05
      • 2011-01-04
      • 1970-01-01
      • 2019-01-11
      • 1970-01-01
      相关资源
      最近更新 更多