2013-03-15 135 views
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道,”这个值之间都存在和逗号(,)CSV文件的默认字段分隔符是逗号( ,所以它会假设为A-42AdarshNagar-2 cg Road作为不同的字段值。我如何解决它?诺基亚CSV文件导入问题

我的PHP代码:

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

您的测试对我来说工作正常。逗号在一个带引号的字符串中,所以它不应该是一个问题。 – Travesty3 2013-03-15 13:00:52

回答

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);