如何使用php获取网站的标题和描述?

发布于 2024-12-08 21:35:46 字数 306 浏览 5 评论 0原文

可能的重复:
帮助获取元标题和描述

我花了一整天的时间。网上搜了一下。在 satckoverflow 上也看到了一些类似的问题。但我得到的却是失望。

我想获得一些 php 代码,通过它我可以输出标题和一些 4-5 行的内容,用于使用 php 的任何网站的描述。

Possible Duplicate:
Help getting meta title and description

I've spend a full day on it. Searched on the net. Saw some similar questions on satckoverflow also. but all I got disappointment.

I want to get some php code by which I can output the title and some 4-5 lines for the description of any website using php.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

擦肩而过的背影 2024-12-15 21:35:46
<?php

  $url = "http://www.drquincy.com/";

  $fp = fopen($url, 'r');
  $content = "";
  while(!feof($fp)) {
        $buffer = trim(fgets($fp, 4096));
        $content .= $buffer;
  }


  $start = '<title>';
  $end = '</title>';
  preg_match(" / $start( . * )$end / s", $content, $match);
  $title = $match[1];
  $metatagarray = get_meta_tags($url);
  $keywords = $metatagarray["keywords"];
  $description = $metatagarray["description"];
  echo " <div><strong>URL: </strong >$url</div> \n";
  echo " <div><strong>Title: </strong >$title</div> \n";
  echo " <div><strong>Description: </strong >$description</div>\n";
  echo " <div><strong>Keywords: </strong >$keywords</div>\n";

只需更改网址即可:)

<?php

  $url = "http://www.drquincy.com/";

  $fp = fopen($url, 'r');
  $content = "";
  while(!feof($fp)) {
        $buffer = trim(fgets($fp, 4096));
        $content .= $buffer;
  }


  $start = '<title>';
  $end = '</title>';
  preg_match(" / $start( . * )$end / s", $content, $match);
  $title = $match[1];
  $metatagarray = get_meta_tags($url);
  $keywords = $metatagarray["keywords"];
  $description = $metatagarray["description"];
  echo " <div><strong>URL: </strong >$url</div> \n";
  echo " <div><strong>Title: </strong >$title</div> \n";
  echo " <div><strong>Description: </strong >$description</div>\n";
  echo " <div><strong>Keywords: </strong >$keywords</div>\n";

Just change the url:)

梦里°也失望 2024-12-15 21:35:46

解析 HTML 的方法有很多种。首先,您需要内容本身:

$res = file_get_contents("http://www.google.com");

假设允许 file_get_contents 访问 uri。
例如,您可以使用正则表达式:

preg_match("~<title>(.*?)</title>~", $res, $match);
$title = $match[1];

但最好使用 DOM 解析器。 http://php.net/manual/en/book.xml.php ,但如果目标内容不是有效的 xml,这可能会出现问题。

There are many ways to parse a HTML. First, you want the content itself:

$res = file_get_contents("http://www.google.com");

That assumes file_get_contents allowed to access uri.
For example, you might utilize a regex:

preg_match("~<title>(.*?)</title>~", $res, $match);
$title = $match[1];

But it would be better to use a DOM parser. http://php.net/manual/en/book.xml.php, though that may be problem if the target content is not valid xml.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文