我已经能够使用 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