一个自动读取unl数据文件的自定义函数代码
在informax数据库中用sql提出了一些数据,默认是unl后辍,在excel中需要分列显示,但在网页上如何才能显示出来并不出码码呢,(有汉字哦,于是乱搞了一个代码,v如数据文件为data.unl 可以试试:
<?
function fgetcsv_reg(& $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;
}
echo '<tr>';
$file_target ='data.unl';
$handle = fopen( $file_target, 'r');
while ($data = fgetcsv_reg($handle, 1000, "|")) {
$num = count($data);
for ($c=0; $c < $num-1; $c++) {
echo "<td>$data[$c]</td>";
}
echo '</tr>';
}
echo "</tr></table>";
fclose($handle);
?>
评论 (0)