freelance84 Posted May 24, 2010 Share Posted May 24, 2010 Users are able to add single line comments to a page. If the last character is a full stop I want to be able to remove it. After researching the substr I thought this would work: if(substr($get_comment_to_add, -1) == ".") { substr_replace($get_comment_to_add ,"",-1); } I know the "substr" bit in the "if" returns the last character. So I am assuming the =="." is incorrect. I have tried escaping with a backslash but it still didn't work. Any ideas anyone? Also I cannot use str_replace because it is only the last period i want to remove, not any others in between. Link to comment https://forums.phpfreaks.com/topic/202768-removing-the-last-if-its-a-period/ Share on other sites More sharing options...
Bladescope Posted May 24, 2010 Share Posted May 24, 2010 rtrim($get_comment_to_add, '.'); No need for the if check. Furthermore, this will remove multiple full stops too. Link to comment https://forums.phpfreaks.com/topic/202768-removing-the-last-if-its-a-period/#findComment-1062721 Share on other sites More sharing options...
freelance84 Posted May 25, 2010 Author Share Posted May 25, 2010 Hi, yea i got it to work with the folloing: if(substr($get_comment_to_add, -1) == ".") { $com_prt1 = substr_replace($get_comment_to_add ,"",-1); } else{ $com_prt1 = $get_comment_to_add; } This takes out the last character if it's a period and leaves in any others in between Link to comment https://forums.phpfreaks.com/topic/202768-removing-the-last-if-its-a-period/#findComment-1062918 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.