如何回显具有标题或元标记的文本文件中的行?

时间:2013-02-24 09:20:04

标签: php

我有一个文本文件,如下所示:

 http://tisue.net/jandek/live.html
<title>Jandek: Live</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">


http://news.nationalgeographic.com/news/2004/06/0629_040629_camelspider.html
<title>Camel Spiders: Behind an E-Mail Sensation From Iraq</title>
<meta name="description" content="A photo of a U.S. soldier in Iraq holding massive,   hairy, supposedly flesh-eating spiders has been burning up e-mail in-boxes around the  world. The arachnids (called camel spiders or wind scorpions) are real, but scientists say many claims about them are anything but.">

还有几千个这样的人。

我正在使用fopen()fgets()来读取每一行并回显它。问题是<title><meta />没有得到回应;这些行返回空白。我如何让php看到这些作为纯文本并回应它们?

编辑:这是我正在使用的代码:

 $handle = fopen($myFile, 'r');

 while(!feof($handle)){

   $data = fgets($handle);
   echo $data;

2 个答案:

答案 0 :(得分:3)

您需要查看该网页的源代码才能看到<title><meta>标记。这可以通过右键单击页面并在大多数浏览器中单击view-source来实现。

或者,您可以转换标记,以便使用htmlspecialchars

在html中读取它们
$handle = fopen($myFile, 'r');

while(!feof($handle)){

    $data = fgets($handle);
    echo htmlspecialchars($data);

答案 1 :(得分:0)

首先需要检查天气,页面上有您想要退出的元标记。 可以使用get_meta_tags(),更多信息http://php.net/manual/en/function.get-meta-tags.php

来完成

为了读取每一行,使用file()逐行读取。

<?php
$lines = file("fileurl");
foreach($lines as $line)
{
    echo($line);
}
?>