Txt to Array on newline

Discussion in 'PHP' started by fireflyproject, Mar 1, 2009.

  1. #1
    I'm trying to get a very simple text file into an array in PHP.

    Here is a sample of the type of text files I'll be parsing through.

    Here is the code I am using to try to get it into an array.

    function parse_txt($file) {
    		$handle = fopen($file, "r");
    		$content = fread($handle, filesize($file));
    		fclose($handle);
    		
    		$array = explode('\r\n', $content);
    		
    		return $array;
    	}
    PHP:
    The way I understand newlines, the \r\n string should locate all carriage return/newlines and explode them.

    Any ideas why this isn't happening?

    Thanks!
     
    fireflyproject, Mar 1, 2009 IP
  2. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #2
    1. \r,\n characters handled only in the double quoted strings, not in the single-quoted
    2. You can use the file function instead of all of your code
     
    wmtips, Mar 1, 2009 IP
  3. fireflyproject

    fireflyproject Active Member

    Messages:
    969
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    70
    #3
    Ah... Double quotes. Should'a figured it'd be something like that!

    Thanks for the recommendation for the file function. I'll check that out.
     
    fireflyproject, Mar 1, 2009 IP