0
Title,"First name","Middle name","Last name","address"
Mr.,"prince","M","Kachhadiya","A-42,AdarshNagar-2
    c.g Road,
"

地址字段的值类似于“A-42,AdarshNagar-2 ChhpraBhatha Road”,该值之间有逗号(,),csv文件默认字段分隔符是逗号(,),因此它将假定A-42AdarshNagar-2 cg Road作为不同的字段值。我该如何解决?

我的PHP代码:

while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
       // Code for parse csv data
}
4

2 回答 2

0

使用 fgetcsv() 从文件中读取。参考http://www.w3schools.com/php/func_filesystem_fgetcsv.asp

于 2013-03-15T12:32:44.433 回答
0
Title,"First name","Middle name","Last name","address"
Mr.,"prince","M","Kachhadiya","A-42,AdarshNagar-2    c.g Road,"

//You can't split the strings using CSV functions so better do some manual work

//All csv values are quoted with double quotes so split the string like this

//**If file size is minimum use this function**

$data = file_get_contents('<file_path>');

$parsed_string = explode('",', $data);

$cnt = count($parsed_string);

for($i=0; $i<$cnt; $i++)
{   
    $value = substr($parsed_string,1);
}

//**If file size is maximum use this**

$file = fopen("<file path>","r");

while(! feof($file))
  {
    $data = fgets($file);

    $parsed_string = explode('",', $data);

    $cnt = count($parsed_string);

    for($i=0; $i<$cnt; $i++)
    {

        $value = substr($parsed_string,1);
    }
  }

fclose($file);
于 2013-03-15T14:21:04.593 回答