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? Link to comment https://forums.phpfreaks.com/topic/35131-removing-multiple-commas-with-recursion-someone-please-help/ 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. Link to comment https://forums.phpfreaks.com/topic/35131-removing-multiple-commas-with-recursion-someone-please-help/#findComment-165826 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 Link to comment https://forums.phpfreaks.com/topic/35131-removing-multiple-commas-with-recursion-someone-please-help/#findComment-165829 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. Link to comment https://forums.phpfreaks.com/topic/35131-removing-multiple-commas-with-recursion-someone-please-help/#findComment-165835 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 Link to comment https://forums.phpfreaks.com/topic/35131-removing-multiple-commas-with-recursion-someone-please-help/#findComment-165837 Share on other sites More sharing options...
discostudio Posted January 21, 2007 Author Share Posted January 21, 2007 Would you like to elaborate please. Link to comment https://forums.phpfreaks.com/topic/35131-removing-multiple-commas-with-recursion-someone-please-help/#findComment-165863 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, ',');?> Link to comment https://forums.phpfreaks.com/topic/35131-removing-multiple-commas-with-recursion-someone-please-help/#findComment-165864 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? Link to comment https://forums.phpfreaks.com/topic/35131-removing-multiple-commas-with-recursion-someone-please-help/#findComment-167009 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.