Jump to content

white space in array from text file


Kane250

Recommended Posts

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);

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.