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还有双引号包着?
这是什么问题?

解决方案 »

  1.   

    <?php
    $handle=fopen('date.csv','r');
    while($data=fgetcsv($handle,10000,'\t', '"')) {   
      echo "$data[0]"."$data[1]"."$data[2]";   
    }
    ?>
      

  2.   

    你的那个 date.csv 是用空格做字段分界符的
      

  3.   

    以下摘自http://www.php.net/manual/en/function.fgetcsv.php的注释The fgetcsv function seems to follow the MS excel conventions, which means:- The quoting character is escaped by itself and not the back slash. 
    (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]";   
    }
    ?>
      

  4.   

    不好意思,是我看错了你的代码是正确的。
    数据文件本身也没有错,不过有 BOM 头。所以会成这样 "ID" NAME EMAILutf-8 的BOM 头为十六进制 ef bb bf 
    因为 php 不能识别 BOM 头,所以他不认为是 " 开头