【问题标题】:nokia csv file import issue in phpphp中的诺基亚csv文件导入问题
【发布时间】:2013-03-15 12:28:21
【问题描述】:
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 c.g Road 为不同的字段值。 我该如何解决?

我的 PHP 代码:

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

【问题讨论】:

  • 你的测试对我来说很好。逗号在带引号的字符串中,所以应该不是问题。

标签: php csv import


【解决方案1】:

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

【讨论】:

    【解决方案2】:
    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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多