Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP end of line character for a csv file

Tags:

php

csv

What is the right way to specify the end of line character of a CSV file with PHP. I have this script to write the CSV file.

<?php

ini_set('display_errors', 1);
error_reporting(E_ALL);

include_once("phpshared.php");

function get_node_urls( $nodeid ) {

$nodes = new SimpleXMLElement('linkcards.xml', null, true);
$nodesarray = $nodes->xpath("//LINKCARD[@ID='$nodeid']");  

$linkstring = '';
$node = $nodesarray[0]; 
$i = 0;
foreach($node->LINKS->LINK as $url)
{ 
       $linkstring .= $i.','.$url['ID'].','.$url->TITLE.','.$url->URL.'\r\n';
       $i++;
}
echo $linkstring;

}

echo get_node_urls(trim($_REQUEST['nodeid']));

?>

If I load $linkstring there is no carriage return at the end of each line. The lines should be:

0, id1, http://link1.com
1, id2, http://link2.com

Instead they are:

0, id1, http://link1.com\r\n1, id2, http://link2.com

The CSV reader reads that line as:

id1 http://link1.com\r\n1

Is there a different way of writing the end of line for a CSV?

like image 867
user823527 Avatar asked Nov 23 '11 23:11

user823527


People also ask

What is the end of line character in CSV?

The files are easily editable using common spreadsheet applications like Microsoft Excel. Fields are separated by commas. Records are separated with system end of line characters, CRLF (ASCII 13 Dec or 0D Hex and ASCII 10 Dec or 0A Hex respectively) for Windows, LF for Unix, and CR for Mac.

How do I insert a line break in a CSV file?

Windows standard CR+LF or Unix-like systems (Linux, Mac) standard LF may be used as new line character.

Do CSV files have newline characters?

Does csv end with newline? CSV is a delimited data format that has fields/columns separated by the comma character and records/rows terminated by newlines.

How do I read the first line of a CSV file in PHP?

php $row = 1; if (($handle = fopen("ab. csv", "r")) !== FALSE) { while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $num = count($data); echo "<p> $num fields in line $row: <br /></p>\n"; $row++; for ($c=0; $c < $num; $c++) { echo $data[$c] .


2 Answers

\r\n needs to be enclosed in " instead of ' so that escape sequences will be interpreted (see documentation):

$linkstring .= $i.','.$url['ID'].','.$url->TITLE.','.$url->URL."\r\n";
like image 140
Tim Cooper Avatar answered Sep 24 '22 09:09

Tim Cooper


After look all over the web I found that the problem is due the Auto Detect Line Ending not being enable. Just set it on your code and it should work. It worked for me.

ini_set('auto_detect_line_endings',TRUE);

With the auto_detect enable you can parse the file as a regular file using

$lines = file('your_csv_file.csv');

On my case I'm already parsing the CSV lines using str_getcsc

function parse_my_csv($filename) { 
    $lines = file($filename);
    $data = array();
    for($i=0;$i<count($lines);$i++) { 
       array_push($data, str_getcsv($lines[$i]));
    }
 return $data;
}
like image 40
Nauro Rezende Jr Avatar answered Sep 26 '22 09:09

Nauro Rezende Jr