-6

到目前为止,我有下面的代码,但结果,唯一显示的是页面标题。我正在尝试将下面的文本文件读入数组:

pear|apple|strawberry|lime|elderberry|watermelon|orange|banana|fig|mango|plum

在网页上显示此文本时,我需要爆炸“|” 分隔符并在垂直列表中显示水果。有什么帮助吗?这是我在正文中的代码:

<h1>Sorted Fruits</h1>
<?php
$file = "Exercise5/exercise5.txt";
$FruitList = file($file);
$lines = explode("| ", $c);
foreach ( $lines as $l ) {
    echo $l;
    die();
}
?>
4

3 回答 3

2
$fruits = "pear|apple|strawberry|lime|elderberry|watermelon|orange|banana|fig|mango|plum";
$fruitlist = implode('</li><li>', array_filter(explode('|', $fruits)));
$fruitlist = "
<ul>
  <li>$fruitlist</li>
</ul>
";

http://codepad.org/xJcrN1UG (w/example using ucwordsto capitalize each term)

或者:

$fruits = "pear|apple|strawberry|lime|elderberry|watermelon|orange|banana|fig|mango|plum";
$fruitlist = str_replace('|', '</li><li>', $fruits);
$fruitlist = "
<ul>
  <li>$fruitlist</li>
</ul>
";

前一种方法用 删除空,可能会发生array_filter()类似的事情pear||apple

于 2013-03-30T17:50:59.820 回答
0
<?php
  $string = 'pear|apple|strawberry|lime|elderberry|watermelon|orange|banana|fig|mango|plum';
  $fruits = explode("|", $string);
?>
<ul>
<?php foreach ($fruits as $fruit) : ?>
    <li><?= $fruit; ?></li></li>
<?php endforeach; ?>
</ul>
于 2013-03-30T17:48:33.747 回答
0

简单的任务,但是好的,我为你解决。

$string = "pear|apple|strawberry|lime|elderberry|watermelon|orange|banana|fig|mango|plum" ;
$fruits = explode("|", $string) ;
foreach ($fruits as $fruit){
  echo $fruit . "<br/>" ;
}
于 2013-03-30T17:48:33.183 回答