How can I remove 3 characters at the end of a string in php?

后端 未结 3 1008
自闭症患者
自闭症患者 2021-01-29 21:02

How can I remove 3 characters at the end of a string in php? \"abcabcabc\" would become \"abcabc\"!

相关标签:
3条回答
  • 2021-01-29 21:44

    Just do:

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

    You don't need to use a strlen call, since, as noted in the substr docs:

    If length is given and is negative, then that many characters will be omitted from the end of string

    0 讨论(0)
  • 2021-01-29 21:46
    <?php echo substr($string, 0, strlen($string) - 3); ?>
    
    0 讨论(0)
  • 2021-01-29 22:02
    <?php echo substr("abcabcabc", 0, -3); ?>
    
    0 讨论(0)
提交回复
热议问题