我已经能够使用php从txt文件中提取数据并在浏览器中显示。但是,即使经过多次尝试,我也无法以我想要的方式展示它。每个数据部分都用分号分隔。我提供了txt文件的示例,使用的php代码和我需要的输出 - 以HTML格式输出两列。有人可以帮忙吗?
输入txt文件:
One
Two
Three
Four
Five;
Six
Seven
Eight
Nine
Ten;
使用的Php代码:
function explodeRows($data)
{
$rowsArr = explode(";", $data);
return $rowsArr;
}
function explodeLines($singleLine)
{
$linesArr = explode("\n", $singleLine);
return $linesArr;
}
$filename = "sample.txt";
$handle = fopen($filename, 'r');
$data = fread($handle, filesize($filename));
$rowsArr = explodeRows($data);
for($i=0;$i<count($rowsArr);$i++)
{
$lineDetails = explodeRows($rowsArr[$i]);
echo "<td>" . $lineDetails[0] . "</td>";
echo "<br>";
}
以上代码的输出:
One Two Three Four Five
Six Seven Eight Nine Ten
var_dump($ rowsArr)的输出; :
array(3) { [0]=> string(27) "One Two Three Four Five" [1]=> string(30) " Six Seven Eight Nine Ten" [2]=> string(2) " " }
array(3) { [0]=> string(27) "One Two Three Four Five" [1]=> string(30) " Six Seven Eight Nine Ten" [2]=> string(2) " " }
array(3) { [0]=> string(27) "One Two Three Four Five" [1]=> string(30) " Six Seven Eight Nine Ten" [2]=> string(2) " " }
需要输出:
One Two Six Seven
Three Eight
Four Five Nine Ten