PHP - How to remove all specific characters at the end of a string?


Question

How to remove the last character only if it's a period?

$string = "something here.";
$output = 'something here';
1
80
4/2/2015 9:32:53 AM

Accepted Answer

$output = rtrim($string, '.');

(Reference: rtrim on PHP.net)

143
5/30/2015 5:17:54 AM

using rtrim replaces all "." at the end, not just the last character

$string = "something here..";
echo preg_replace("/\.$/","",$string);

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon