我正在尝试上传excel文件并将其内容存储在Mysql数据库中。
我在保存内容时遇到问题.. 像
我的csv文件采用
的形式 "1","aruna","IEEE
paper"
"2","nisha","JOurnal magazine"
实际上我有2条记录而我正在使用代码
<?php
$string = file_get_contents( $_FILES["file"]["tmp_name"] );
//echo $string;
foreach ( explode( "\n", $string ) as $userString )
{
echo $userString;
}
&GT?; 因为在Csv记录中,在IEEE和纸张之间插入了一条新行,它将我作为3条记录发送给我。
如何明智地删除这个新行代码并修改代码,以便只考虑记录1和2之间的新行......
答案 0 :(得分:2)
使用fgetcsv加载解析CSV文件。
答案 1 :(得分:1)
也许您应该使用库来打开和修改Excel文件。试试XLS Reader。
答案 2 :(得分:1)
fgetcsv命令功能强大且有用,您应该使用它。但是如果你想要回答原来的问题......
<?php
$txt = <<<_TXT
"1","aruna","IEEE
paper" "2","nisha","JOurnal magazine"
"2","aruna","IEEE paper" "2","nisha","JOurnal magazine"
"3","aruna","IEEE paper" "2","nisha","JOurnal
magazine"
"4","aruna","IEEE paper" "2","nisha","JOurnal magazine"
_TXT;
$txt = str_replace("\"\n", "@EOL@", $txt);
$txt = str_replace("\n", "@NL@", $txt);
$txt = str_replace("@EOL@", "\n", $txt);
echo $txt;
?>
输出是......
“1”,“aruna”,“IEEE @ NL @ paper”“2”,“nisha”,“JOurnal magazine
“2”,“aruna”,“IEEE论文”“2”,“nisha”,“JOurnal magazine
“3”,“aruna”,“IEEE paper”“2”,“nisha”,“JOurnal @ NL @ magazine
“4”,“aruna”,“IEEE论文”“2”,“nisha”,“JOurnal magazine”