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

03/25/2020 01:30:02

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

Verified Answer (654 Votes)

02/07/2011 01:10:27

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

654

Answer #2 (30 Votes)

02/07/2011 01:09:10

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

30

Answer #3 (6 Votes)

02/07/2011 01:08:02
<?php echo substr($string, 0, strlen($string) - 3); ?>
6
3
Hack Hex uses Stack Exchance API by the Stack Exchange Inc. to scrape questions/answers under Creative Commons license.