brian914 Posted May 25, 2011 Share Posted May 25, 2011 I have the following function, which takes a string with commas in it and attempts remove those commas. The way I have it here is that I use explode to take out the commas, which makes an array, and then iterate through that array to put a string back together without the commas. function tags_to_sort_by( $sortMeta ) { $sortByArray = explode(",", $sortMeta); $sortByCount = count($sortByArray); for ($i = 0; $i < $sortByCount; $i++) { $sortByString += $sortByArray[$i]; } return $sortByString; } What does not work is the following line: $sortByString += $sortByArray[$i]; Somehow that outputs a 0, which I don't understand. It should output something like: arrayItem1 arrayItem2 array3 etc. My question is if there either is an easier way to remove the commas from the original string or what I am doing wrong in this line: $sortByString += $sortByArray[$i]; // I am trying to concatenate each part of the array back into the string. Thanks a lot for help with this! Quote Link to comment https://forums.phpfreaks.com/topic/237384-removing-commas-from-a-string-and-keeping-it-a-string/ Share on other sites More sharing options...
gizmola Posted May 25, 2011 Share Posted May 25, 2011 Yes there is an easier way. str_replace Quote Link to comment https://forums.phpfreaks.com/topic/237384-removing-commas-from-a-string-and-keeping-it-a-string/#findComment-1219830 Share on other sites More sharing options...
brian914 Posted May 25, 2011 Author Share Posted May 25, 2011 Perfect! Thank you so much! Quote Link to comment https://forums.phpfreaks.com/topic/237384-removing-commas-from-a-string-and-keeping-it-a-string/#findComment-1219835 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.