试图在php中创建一个无序列表

时间:2014-05-19 19:49:18

标签: php html text

我是php的新手,我正在尝试编写一个小应用程序来读取文本文件并使其成为无序列表。我的目标是使文本文件更具可读性,因此我想使标题变粗,并简单地使列出的项目具有很少的标记(如果它们在无序列表中则应该如此)。我写了这样做的PHP,但它没有做我所期望的。相反,它需要每一行,并使其变得越来越大,直到它变得如此之大以至于难以理解。我在这里看不出我做错了什么。有没有人看到我遗失或写错的东西?下面我分别粘贴了原始文本文件和我的php代码。感谢您对我有限的PHP知识的帮助和耐心。

Best Picture
American Hustle
Captain Phillips
Dallas Buyers Club
Gravity
Her
Nebraska
Philomena
12 Years a Slave
The Wolf of Wall Street

Best Actor
Christian Bale (American Hustle)
Bruce Dern (Nebraska)
Leonardo DiCaprio (The Wolf of Wall Street)
Chiwetel Ejiofor (12 Years a Slave)
Matthew McConaughey (Dallas Buyers Club)

Best Actress
Amy Adams (American Hustle)
Cate Blanchett (Blue Jasmine)
Sandra Bullock (Gravity)
Judi Dench (Philomena)
Meryl Streep (August: Osage County)

** 3个标题应该是最佳画面,最佳演员和最佳女演员。

**以下是我的PHP代码:

<html>
<head>
<title>Un-ordered List</title>
</head>
<body>
<?php
$file = fopen("oscars.txt", "r");
$i=0;
while(!feof($file))
{
$members[]= fgets($file);
}
fclose($file);
$arrlength =count($members);
$title = True;
echo $members;
for($i=0;$i<($arrlength);$i++)
{
    if($title=True)
    {
        echo "<h2>" . $members[$i] . "<h2><ul>";
        $title = False;
    }
    if(trim($members[$i])=='')
    {
        echo "</ul><h2>" . $members[$i+1] . "</h2><ul>";
        $i++;
    }
    else 
    { 
        echo "<li>" . $members[$i] . "</li>" ;
    }
}
?>
</body>
</html>

3 个答案:

答案 0 :(得分:2)

=更改为==中的if。并将第二个<h2>更改为</h2>

但是为了紧凑,请考虑以下代码:

echo "<html><head><title>Un-ordered List</title></head><body>";
foreach( array_map("htmlspecialchars", file("oscars.txt")) as $line ) {
  if (!isset($flag)) {
    $flag = true;
    echo "<h2>{$line}</h2><ul>";
  } else {
    echo "<li>{$line}</li>";
  }
}
if (isset($flag)) {
  echo "</ul>";
}
echo "</body></html>";

答案 1 :(得分:2)

一旦你对PHP变得更加自信,我建议你阅读MySQL和数据库,因为它更适合这种数据存储。但是,我已设法找到问题的解决方案,但它涉及在文本文件中的每个标题的开头添加星号(*)。这样代码可以确定哪些行是标题,哪些不是。

<html>
<head>
    <title>Un-ordered List</title>
</head>

<body>
    <?php
        $members = file("oscars.txt"); // load file into array

        foreach($members as $member) // assign each object in the array to $members on each iteration
        {
            $member = trim($member); // perhaps unnecessary, but good practise

            if(substr($member, 0, 1) == '*') // if there is an asterisk, it is a header
            {
                echo "<h2>" . substr($member, 1, strlen($member)) . "</h2>"; // print the heading, minus the asterisk
            }
            else if ($member !== '') // don't print blank lines
            {
                echo "<li>" . $member . "</li>"; // not a heading, so print as a list element
            }
        }
    ?>
</body>
</html>

我几乎完全重写了代码,但我觉得你写的一些内容是不必要的,例如当$arrlength =count($members)循环更适合这项工作时使用foreach。它将从$members数组中获取每个字符串,然后将其输出以代替$member变量。

我也改变了一些格式,但这并不重要。

答案 2 :(得分:0)

<html>
<head>
<title>Un-ordered List</title>
</head>
<body>
<?php
$file = fopen("oscars.txt", "r");

$i=1;
while ($line = fgets($file)) {
    if(strlen(trim($line)) == 0){ 
        print "</ul>";
        $i=0;
    }  

    if ($i !=0){
        if ($i==1) print "<b>".$line."</b>";
        else {
            if ($i==2) print "<ul><li>".$line."</li>";
            else print "<li>".$line."</li>";
        }
    }

    $i++;   
}
?>
</body>
</html>

结果是:

最佳影片

  • American Hustle
  • 菲利普斯船长
  • 达拉斯买家俱乐部
  • 重力
  • 她的
  • 内布拉斯加
  • 菲洛梅娜
  • 12年奴隶
  • 华尔街之狼

最佳男主角

  • Christian Bale(美国喧嚣)
  • Bruce Dern(内布拉斯加州)
  • 莱昂纳多迪卡普里奥(华尔街之狼)
  • Chiwetel Ejiofor(12岁奴隶)
  • Matthew McConaughey(达拉斯买家俱乐部)

最佳女主角

  • 艾米亚当斯(美国喧嚣)
  • 凯特布兰切特(蓝茉莉花)
  • 桑德拉布洛克(重力)
  • Judi Dench(Philomena)
  • 梅丽尔斯特里普(8月:欧塞奇县)