假设我有一个txt文件,里面有这样的信息示例:
amy,anderson,aldergrove,archery,anchovies,110
bill,bonds,burnaby,bowling,beer,100
cameron,carson,cameroon,cars,candy,120
henry,henderson,harrison,helping,hamburgers,90
dorothy,dust,denmark,driving,drinks,80
ed,edmunson,edmonton,eating,eggs,77
fred,fredrickson,fernie,flying,fries,140
我想使用file()和preg_split()函数来调用它,并将其显示为一个表,最简单的方法是什么?我知道如何使用file()函数调用它,但我不知道如何替换,并使它看起来像一个表。
http://et4891.site90.com/sample.jpg<---这是我想要的样子的一个样本。
下面是我调用txt文件时所做的操作。
<?php
$fileContentsArray = file("aaa.txt");
echo "<table>";
foreach($fileContentsArray as $one_persons_data)
{
echo "<tr>$one_persons_data</tr>";
}
echo "</table>"
?>
我应该如何修改它,使其看起来像我发布的图像?
提前感谢。。。。
是否需要preg_split?在这种情况下最好使用爆炸。总之:
<?php
$fileContentsArray = file("aaa.txt");
echo "<table>";
foreach($fileContentsArray as $one_persons_data)
{
echo '<tr>';
$splitted = preg_split('/,/', $one_persons_data);
foreach ($splitted as $one) {
echo "<td>$one</td>";
}
echo '</tr>';
}
echo "</table>"
您可以这样做:
<?php
$rows = file('data.txt');
echo '<table>';
foreach($rows as $row){
echo '<tr>';
foreach(explode(',',$row) as $field){
echo '<td>';
echo htnlentities($field);
echo '</td>';
}
echo '</tr>';
}
echo '</table>';
希望这能帮助到你。