2

我试图提取 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";

  ?>
4

1 回答 1

2

你可以这样做

$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');
于 2013-05-22T02:34:50.943 回答