csv文件的PHP行尾字符
使用PHP指定CSV文件的行尾字符的正确方法是什么。我有这个脚本来编写CSV文件csv文件的PHP行尾字符,php,csv,Php,Csv,使用PHP指定CSV文件的行尾字符的正确方法是什么。我有这个脚本来编写CSV文件 <?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 = $
<?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']));
?>
相反,它们是:
0, id1, http://link1.com\r\n1, id2, http://link2.com
0,id1,http://link1.com\r\n1,id2,http://link2.com
CSV阅读器将该行读取为:
id1 http://link1.com\r\n1
id1http://link1.com\r\n1
是否有其他方式为CSV写入行尾?
\r\n
需要包含在“
而不是”
中,以便解释转义序列():
在浏览了整个网页后,我发现问题是由于自动检测行尾未启用。只需在代码中设置它,它就会工作。它对我起作用
ini_set('auto_detect_line_endings',TRUE);
使用auto_detect enable,您可以使用
$lines = file('your_csv_file.csv');
在我的例子中,我已经在使用str_getcsc解析CSV行了
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;
}
函数parse_my_csv($filename){
$lines=文件($filename);
$data=array();
对于($i=0;$i+1 Arrggghhhh!非常感谢,PHP中的行尾处理非常可笑。
$lines = file('your_csv_file.csv');
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;
}