[php]fgetcsv 讀入檔案無「「」時,第一字元被刪掉亂碼

回覆文章
布魯斯
Site Admin
文章: 211
註冊時間: 週四 2月 16, 2006 3:34 pm

[php]fgetcsv 讀入檔案無「「」時,第一字元被刪掉亂碼

文章 布魯斯 »

在使用 fgetcsv 讀取 CSV 檔案時,如該 CSV 檔案的中文字有「「」時文字是可以正常的,
但在一般情況無「"」,則會出現第一個字元被吃掉的情況,以致出現亂碼。

在網路上的資料多半是建議使用 setlocale 來解決問題
語法如下

// utf-8
setlocale ( LC_ALL, 'en_US.UTF-8' ) ;
// big5
setlocale ( LC_ALL, 'zh_TW.BIG5' ) ;

但實際情況,並不會解決問題,
目前較好的方式是使用另外寫的 function 來處理這問題

function __fgetcsv(&$handle, $length = null, $d = ",", $e = '"')
{
$d = preg_quote($d);
$e = preg_quote($e);
$_line = "";
$eof=false;
while ($eof != true)
{
$_line .= (empty ($length) ? fgets($handle) : fgets($handle, $length));
$itemcnt = preg_match_all('/' . $e . '/', $_line, $dummy);
if ($itemcnt % 2 == 0)
$eof = true;
}
$_csv_line = preg_replace('/(?: |[ ])?$/', $d, trim($_line));

$_csv_pattern = '/(' . $e . '[^' . $e . ']*(?:' . $e . $e . '[^' . $e . ']*)*' . $e . '|[^' . $d . ']*)' . $d . '/';
preg_match_all($_csv_pattern, $_csv_line, $_csv_matches);
$_csv_data = $_csv_matches[1];

for ($_csv_i = 0; $_csv_i < count($_csv_data); $_csv_i++)
{
$_csv_data[$_csv_i] = preg_replace("/^" . $e . "(.*)" . $e . "$/s", "$1", $_csv_data[$_csv_i]);
$_csv_data[$_csv_i] = str_replace($e . $e, $e, $_csv_data[$_csv_i]);
}
return empty ($_line) ? false : $_csv_data;
}
參考的網站為:http://blog.csdn.net/shilian_h/archive/ ... 71051.aspx
回覆文章