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? Quote 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) Quote 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; } Quote 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 Quote 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 Quote 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
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.