我是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>
更改=
到==
的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>";
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句