fgetcvs在linux的问题
前端之家收集整理的这篇文章主要介绍了
fgetcvs在linux的问题,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
看到有人在有汉字的字符串 前加一个 ‘ 或是任意半角符号,让bug将其除掉,不过这样做太麻烦了。最后呢,找来一个模拟fgetcsv功能的函数。
<div class="codetitle"><a style="CURSOR: pointer" data="60595" class="copybut" id="copybut60595" onclick="doCopy('code60595')"> 代码如下:
<div class="codebody" id="code60595">
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;
}