在php中的nokia csv文件导入问题

时间:2013-03-15 12:28:22

标签: php csv import

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-42 AdarshNagar-2 cg Road 作为不同的字段值。 我怎么解决它?

我的Php代码:

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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);