discostudio Posted January 21, 2007 Share Posted January 21, 2007 [code]$string = "Hiil Road, Peterborough, , , , ";function remove_commas($str) { if ( substr($str, -2, 2) === ", ") { $str = substr($str, 0, -2); echo $str."<br />"; } if(substr($str, -2, 2) === ", ") remove_commas($str); else return $str;}[/code]In theory the above function should work! - Can someone help? Quote Link to comment Share on other sites More sharing options...
Orio Posted January 21, 2007 Share Posted January 21, 2007 As far as I can see, it should work. Maybe change the === operator to ==.In general, recursion is not really recommended with php. Try using a loop instead (again, replace === with == if it doesn't work and give it a shot):[code]<?phpfunction remove_commas($str){ while(substr($str, -2, 2) === ", ") $str = substr($str, 0, -2); return $str;}?>[/code]Orio. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted January 21, 2007 Share Posted January 21, 2007 Why won't using str_replace work?[code]<?php$string = "Hiil Road, Peterborough, , , , ";$new_str = str_replace(', ','',$string);echo $new_str;?>[/code]Ken Quote Link to comment Share on other sites More sharing options...
discostudio Posted January 21, 2007 Author Share Posted January 21, 2007 [quote author=kenrbnsn link=topic=123408.msg509996#msg509996 date=1169416217]Why won't using str_replace work?[code]<?php$string = "Hiil Road, Peterborough, , , , ";$new_str = str_replace(', ','',$string);echo $new_str;?>[/code]Ken[/quote]Thanks for the reply, but that would remove the , betwen "Hiil Road, Peterborough" and I need that to stay there. The point of this is that I could also have a varibale as follows:"Hill Road, Peterborough, London, Essex, , , , , , "I just need to remove the trailing commas. regardless of how many there may be. Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 21, 2007 Share Posted January 21, 2007 use trim() passing "," as the extra chars.http://us3.php.net/manual/en/function.trim.php Quote Link to comment Share on other sites More sharing options...
discostudio Posted January 21, 2007 Author Share Posted January 21, 2007 Would you like to elaborate please. Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 21, 2007 Share Posted January 21, 2007 Not really...that was pretty much it. <?$string = "Hiil Road, Peterborough, , , , ";echo trim($string, ',');?> Quote Link to comment Share on other sites More sharing options...
discostudio Posted January 23, 2007 Author Share Posted January 23, 2007 It doesn't work. trim($string, ","); does literally nothing...Any ideas? Quote Link to comment 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.