user502039
user502039

Reputation:

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"!

Upvotes: 396

Views: 469831

Answers (3)

bensiu
bensiu

Reputation: 25564

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

Upvotes: 867

KomarSerjio
KomarSerjio

Reputation: 2911

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

Upvotes: 52

Jan
Jan

Reputation: 2293

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

Upvotes: 5

Related Questions