0

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>"
?>

投稿した画像のように見えるようにするには、これをどのように変更すればよいですか?

事前に感謝します....

4

2 に答える 2

1

preg_split は必要ですか? この場合、explode を使用することをお勧めします。ともかく:

<?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>"
于 2013-02-11T05:20:35.203 に答える
0

あなたはこれを行うことができます:

<?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>';

これがお役に立てば幸いです。

于 2013-02-11T05:15:46.793 に答える