ChatGPT解决这个技术问题 Extra ChatGPT

How can I remove three characters at the end of a string in PHP?

How can I remove three characters at the end of a string in PHP?

"abcabcabc" would become "abcabc"!

You might find s($str)->cutEnd(3) helpful, as found in this standalone library. This is Unicode or multibyte safe.

P
Peter Mortensen

Just do:

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

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

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


y
ya_Bob_Jonez
<?php echo substr("abcabcabc", 0, -3); ?>

J
Jan
<?php echo substr($string, 0, strlen($string) - 3); ?>

The strlen() is unnecessary.