时间:2021-07-01 10:21:17 帮助过:13人阅读
common.php
public static function readTabFile($fileName)
{
if (!file_exists($fileName))
{
die("404 File not found!");
}
$handle = fopen($fileName, 'rb');
$contents = fread($handle, filesize ($fileName));
//iconv("UTF-8","GBK", $contents);
if(ord($contents) == 0xEF)
{
$contents = substr($contents, 4);
}
$rows = explode("\r\n", $contents);
fclose($handle);
$ret;
$colNames = null;
for($i = 0; $i < count($rows); $i++)
{
$row = $rows[$i];
if(!empty($row) && $row != "" && substr($row, 0, 1) != "#")
{
$col = explode("\t", $row);
if(empty($colNames) || $colNames == null)
$colNames = $col;
else
{
$item;
$itemId = (int)$col[0];
if(empty($itemId))
{
die("...");
}
for($j = 0; $j < count($col); $j++)
{
$value = $col[$j];
$item[$colNames[$j]] = $value;
}
$ret[$itemId] = $item;
}
}
}
return $ret;
}
?>
test.php
$dir = "../goods.tab";
$table = Common::readTabFile($dir);
?>