Created
August 4, 2011 01:23
-
-
Save kijtra/1124306 to your computer and use it in GitHub Desktop.
[PHP] fgetcsvの文字化けを解消する代替関数。yossyさんの記事にある素晴らしいスニペットを、記事コメントをもとに少し精査。 http://yossy.iimp.jp/wp/?p=56
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
yossyさんの記事にある素晴らしいスニペットを、コメントをもとに少し精査。 | |
http://yossy.iimp.jp/wp/?p=56 | |
*/ | |
/** | |
* ファイルポインタから行を取得し、CSVフィールドを処理する | |
* @param resource handle | |
* @param int length | |
* @param string delimiter | |
* @param string enclosure | |
* @return ファイルの終端に達した場合を含み、エラー時にFALSEを返します。 | |
*/ | |
function fgetcsv_reg(&$handle,$length=NULL,$d=',',$e='"'){ | |
$d=preg_quote($d); | |
$e=preg_quote($e); | |
$_line=""; | |
$eof=false; | |
while(($eof!=true) && (!feof($handle))){ | |
$_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('/(?:\\r\\n|[\\r\\n])?$/',$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; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment