【问题标题】:How to extract Title and description of a url using php如何使用 php 提取 url 的标题和描述
【发布时间】:2013-05-22 02:28:44
【问题描述】:

我试图提取 url 的标题、描述和关键字。下面的代码适用于单行描述,但不适用于多行描述。任何人都可以帮助解决这个问题。如果我能像社交书签网站一样知道如何提取图像,那就太好了。

<?php

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

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


    }


  $start = '<title>';
  $end = '</title>';
  preg_match('!<title>(.*?)</title>!i', $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";

  ?>

【问题讨论】:

  • 也许这行得通:!&lt;title&gt;(.*?)&lt;/title&gt;!im,带有多行修饰符。

标签: php url title extraction


【解决方案1】:

你可以这样做

$doc = new DOMDocument;
$doc->loadHTMLFile('http://urlhere.com');
$title = $doc->getElementsByTagName('title');
$title = $title[0];
$xpath = new DOMXPath($doc);
$description = $xpath->query('/html/head/meta[name@="description"]/@content');
$keywords = $xpath->query('/html/head/meta[name@="keywords"]/@content');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-08
    • 1970-01-01
    • 1970-01-01
    • 2017-08-15
    • 2011-12-03
    • 1970-01-01
    • 2012-02-08
    • 2023-02-05
    相关资源
    最近更新 更多