etrader Posted March 11, 2011 Share Posted March 11, 2011 How to remove the last character of a string if it is "-" ? In replacing some elements in strings, sometimes "-" remains at the end. How to trim the string by removing "-", if it is at the end of the string? Link to comment https://forums.phpfreaks.com/topic/230290-deleting-the-last-character-of-a-string/ Share on other sites More sharing options...
Mahngiel Posted March 11, 2011 Share Posted March 11, 2011 str_replace("~", "", $string) Link to comment https://forums.phpfreaks.com/topic/230290-deleting-the-last-character-of-a-string/#findComment-1185937 Share on other sites More sharing options...
ldb358 Posted March 11, 2011 Share Posted March 11, 2011 this should work function trimdash($string){ if($string{strlen($string)-1} == '-'){ $string = substr($string,0,-1); } return $string; } Link to comment https://forums.phpfreaks.com/topic/230290-deleting-the-last-character-of-a-string/#findComment-1185944 Share on other sites More sharing options...
kenrbnsn Posted March 11, 2011 Share Posted March 11, 2011 Use rtrim() <?php $test = 'string with-'; echo rtrim($test,'-'); ?> Ken Link to comment https://forums.phpfreaks.com/topic/230290-deleting-the-last-character-of-a-string/#findComment-1185981 Share on other sites More sharing options...
etrader Posted March 11, 2011 Author Share Posted March 11, 2011 wow excellent solution Link to comment https://forums.phpfreaks.com/topic/230290-deleting-the-last-character-of-a-string/#findComment-1186071 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.