2013-03-15 107 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);