Skip to content

Instantly share code, notes, and snippets.

@ShinyChang
Created May 5, 2015 09:22
Show Gist options
  • Save ShinyChang/fb2e4a4fc396fa453c5b to your computer and use it in GitHub Desktop.
Save ShinyChang/fb2e4a4fc396fa453c5b to your computer and use it in GitHub Desktop.
<?php
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));
$_line = mb_convert_encoding($_line, "UTF-8", "BIG-5"); // convert big5 to utf8
$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;
}
$o = __fgetcsv(fopen("some_big5_encoding_file.csv", "r"));
echo "<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title>TEST</title>
</head>
<body>";
echo print_r($o, true);
echo "</body></html>";
帳號 密碼 姓氏 名稱 信箱 單位
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment