date.csv:
"ID" "NAME" "EMAIL"
"1" "小明" "[email protected]"
"2" "小东" "[email protected]"
"3" "小少" "[email protected]"
读取这个csv文件
<?php
$handle=fopen('date.csv','r');
while($data=fgetcsv($handle,10000,"\t"))
{
echo "$data[0]"."$data[1]"."$data[2]";
}
?>读取后在页面上显示时,成了这样:
"ID" NAME EMAIL
1 小明 [email protected]
2 小东 [email protected]
3 小少 [email protected]
fgetcsv函数的字段环绕符默认是双引号,
为什么我读取出来时,其它字段都好好的,可是ID还有双引号包着?
这是什么问题?
"ID" "NAME" "EMAIL"
"1" "小明" "[email protected]"
"2" "小东" "[email protected]"
"3" "小少" "[email protected]"
读取这个csv文件
<?php
$handle=fopen('date.csv','r');
while($data=fgetcsv($handle,10000,"\t"))
{
echo "$data[0]"."$data[1]"."$data[2]";
}
?>读取后在页面上显示时,成了这样:
"ID" NAME EMAIL
1 小明 [email protected]
2 小东 [email protected]
3 小少 [email protected]
fgetcsv函数的字段环绕符默认是双引号,
为什么我读取出来时,其它字段都好好的,可是ID还有双引号包着?
这是什么问题?
解决方案 »
- 关于“用户登录”服务器行为中与数据库不匹配返回值的问题
- file_get_contents读不到网易的内容,有什么良策?
- (php报错)Number of bind variables doesn't mat
- 关于JS中assign的问题
- 显示隐藏的问题,在线等
- 请问php网站的网页页面的一些问题
- 请教一个字符截取的问题
- 急求调查系统源码
- 图片上传,错误◎◎
- 请问怎么样用session_save_path()(注:不是在php.ini中改的),还有就是删除一个session,怎么用session_destroy呀!我查过手册,但是还没
- odbc_fetch_array如何获取数字数组?
- 求1数据库升级代码
$handle=fopen('date.csv','r');
while($data=fgetcsv($handle,10000,'\t', '"')) {
echo "$data[0]"."$data[1]"."$data[2]";
}
?>
(i.e.Let's use the double quote (") as the quoting character:
Two double quotes "" will give a single " once parsed, if they are inside a quoted field (otherwise neither of them will be removed). \" will give \" whether it is in a quoted field or not (same for \\) , and if a single double quote is inside a quoted field it will be removed. If it is not inside a quoted field it will stay).- leading and trailing spaces (\s or \t) are never removed, regardless of whether they are in quoted fields or not.- Line breaks within fields are dealt with correctly if they are in quoted fields. (So previous comments stating the opposite are wrong, unless they are using a different PHP version.... I am using 4.4.0.)So fgetcsv if actually very complete and can deal with every possible situation. (It does need help for macintosh line breaks though, as mentioned in the help files.)I wish I knew all this from the start. From my own benchs fgetcsv strikes a very good compromise between memory consumption and speed.-------------------------
Note: If back slashes are used to escape quotes they can easily be removed afterwards. Same for leading and trailing spaces.就是说fgetcsv在读\t的时候有问题,用逗号分割就可以了
"ID","NAME","EMAIL"
"1","小明","[email protected]"
"2","小东","[email protected]"
"3","小少","[email protected]"<?php
$handle=fopen('date.csv','r');
while($data=fgetcsv($handle,10000,',')) {
echo "$data[0]"."$data[1]"."$data[2]";
}
?>
数据文件本身也没有错,不过有 BOM 头。所以会成这样 "ID" NAME EMAILutf-8 的BOM 头为十六进制 ef bb bf
因为 php 不能识别 BOM 头,所以他不认为是 " 开头