Kane250 Posted June 25, 2009 Share Posted June 25, 2009 This is probably the dumbest question, but how can I get rid of all whitespace from an array I am creating from a text file? I have numbers in a text file, separated by , and at the ends of some lines, I have done a soft return because my text editor does weird stuff with indenting when I don't. However, when these numbers are pulled through later on, there is a space before some of the numbers. Here is what I have been trying to use. Doesn't work. Any help is very much appreciated! $numbers = file_get_contents('sample.txt'); $numbers = str_replace("\r\n","",$numbers); $numbers = explode(",", $numbers); Quote Link to comment https://forums.phpfreaks.com/topic/163608-white-space-in-array-from-text-file/ Share on other sites More sharing options...
Daniel0 Posted June 25, 2009 Share Posted June 25, 2009 array_map + trim, or for(each) loop instead of array_map(). Quote Link to comment https://forums.phpfreaks.com/topic/163608-white-space-in-array-from-text-file/#findComment-863245 Share on other sites More sharing options...
Kane250 Posted June 25, 2009 Author Share Posted June 25, 2009 Thanks for the response. I'm really confused about what array_map does, and I thought trim() is just to remove whitespace at the beginning and end, no? I originally had this set up using foreach t make the array and had the same problem. Quote Link to comment https://forums.phpfreaks.com/topic/163608-white-space-in-array-from-text-file/#findComment-863469 Share on other sites More sharing options...
MatthewJ Posted June 25, 2009 Share Posted June 25, 2009 Once you explode the numbers into a new array, you only have white space at the beginning or end, so trim() would be exactly what you want to use I would think. Quote Link to comment https://forums.phpfreaks.com/topic/163608-white-space-in-array-from-text-file/#findComment-863475 Share on other sites More sharing options...
Daniel0 Posted June 25, 2009 Share Posted June 25, 2009 Something like: $numbers = array_map('trim', explode(',', $numbers); or $numbers = explode(',', trim($numbers)); Quote Link to comment https://forums.phpfreaks.com/topic/163608-white-space-in-array-from-text-file/#findComment-863496 Share on other sites More sharing options...
Kane250 Posted June 25, 2009 Author Share Posted June 25, 2009 If there would only be space at the beginning and end after exploding into an array, then why else would I be getting white space in the middle of it? That's my main problem. The white space is coming from the soft returns in my list of numbers. ex: 345,876,987 922, 761 When echoed from the array, the numbers would look like this: 345,876,987, 0922,761. Notice the space? Quote Link to comment https://forums.phpfreaks.com/topic/163608-white-space-in-array-from-text-file/#findComment-863497 Share on other sites More sharing options...
MatthewJ Posted June 25, 2009 Share Posted June 25, 2009 Well, if each array element holds a string of separated values like 111,222,33, 456 and not one single number per array item, just do a str_replace(" ", "", $arrayelement) in a loop. Quote Link to comment https://forums.phpfreaks.com/topic/163608-white-space-in-array-from-text-file/#findComment-863500 Share on other sites More sharing options...
Kane250 Posted June 25, 2009 Author Share Posted June 25, 2009 Something like: $numbers = array_map('trim', explode(',', $numbers); or $numbers = explode(',', trim($numbers)); Ok, that totally worked, ha. I used the first one: $numbers = array_map('trim', explode(',', $numbers)); and it fixed it. I still don't totally get it, but hey, great! Thanks for your help! Quote Link to comment https://forums.phpfreaks.com/topic/163608-white-space-in-array-from-text-file/#findComment-863507 Share on other sites More sharing options...
Kane250 Posted June 25, 2009 Author Share Posted June 25, 2009 Well, if each array element holds a string of separated values like 111,222,33, 456 and not one single number per array item, just do a str_replace(" ", "", $arrayelement) in a loop. Thanks for the suggestion. That's what I was originally doing and it was not removing it, which s why I was so confused. Those numbers were single numbers separated by the , . Anyway the solution above worked out, bu thanks for helping out. Quote Link to comment https://forums.phpfreaks.com/topic/163608-white-space-in-array-from-text-file/#findComment-863510 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.