file_get_contents() not showing new lines

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dgourd
    New Member
    • Apr 2010
    • 25

    file_get_contents() not showing new lines

    I am trying to parse certain excel sheets. I converted them to csv files to strip out any excess formatting. To parse these files, my system is dependant on new lines ("/n"). I use this code to get the data from the uploaded csv file:

    Code:
    $data = file_get_contents($_FILES["file"]["tmp_name"], FILE_USE_INCLUDE_PATH);
    The problem is that any of the new lines in the csv show up as single spaces in $data. I was wondering if there is a way to preserve the new lines in $data.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    How are you viewing the data? Note that HTML does not respect white-space chars, so new-lines and multiple spaces are ignored. Try wrapping them into a <pre> block.

    Also, you may want to check out the fgetcsv function. It reads in a line from a CSV file and returns the values in it as an array.

    Comment

    • dgourd
      New Member
      • Apr 2010
      • 25

      #3
      I am uploading the data through a form. I found by using this:
      Code:
      $data = file($_FILES["file"]["tmp_name"], FILE_USE_INCLUDE_PATH | FILE_IGNORE_NEW_LINES);
      It returns the data as an array with each new line as a separate array entry. Thanks for your help though.

      Comment

      • HaLo2FrEeEk
        Contributor
        • Feb 2007
        • 404

        #4
        Darn, you beat me to it. I was just about to suggest file(), it's helped me quite a few times.

        Comment

        Working...