开发者

Loop separated text into variables

Im using this PHP to display the contents or text files held in a directory, the text files all follow the same format.

$contents = file_get_contents($filename);
     $items = explode('¬', $contents);
     echo '<table width="500" border="1" cellpadding="4">';
     foreach ($items as $item) {
   开发者_如何学编程    echo "<tr><td>$item</td></tr>\n";
     }
     echo '</table>';

There are 7 $items in each text file:

tag,name,description,text1,text2,text3,date

So instead of outputting $item, can i give each its own variable?

Thanks.


Try this:

$contents = file_get_contents($filename);
list($tag, $name, $description, $text1, $text2, $text3, $date) = explode('¬', $contents);

echo '<table width="500" border="1" cellpadding="4">';
echo "<tr><td>$tag</td></tr>\n";
echo "<tr><td>$name</td></tr>\n";
echo "<tr><td>$description</td></tr>\n";
echo "<tr><td>$text1</td></tr>\n";
echo "<tr><td>$text2</td></tr>\n";
echo "<tr><td>$text3</td></tr>\n";
echo "<tr><td>$date</td></tr>\n";
echo '</table>';


Try:

while(list($tag,$name,$desc,$text1,$text2,$text3,$date) = each($items){
    // do something
}


Treat the text file as a csv file seperated by the ¬ char.

http://www.php.net/manual/en/function.fgetcsv.php

Then you can either rely upon a header file to describe the fields ( columns ) or access them numerically.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜