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

PhpString

Php Problem Overview


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

"abcabcabc" would become "abcabc"!

Php Solutions


Solution 1 - Php

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

Solution 2 - Php

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

Solution 3 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser502039View Question on Stackoverflow
Solution 1 - PhpbensiuView Answer on Stackoverflow
Solution 2 - PhpKomarSerjioView Answer on Stackoverflow
Solution 3 - PhpJanView Answer on Stackoverflow