繁体   English   中英

在 php 中,如何从 header 中获取所有链接标签?

[英]In php, how to get all link tags from header?

我们都在网页的 header 中看到/使用过类似的东西:

<link rel="stylesheet" href="https://somedomain.com/">

如何使用 php 从 header 获取所有链接标签?

就像

get_meta_tags(url);

检索元标记。

谢谢,

有多种方法可以抓取页面并对其进行解析。

  1. 你可以使用SimpleHTMLDOM
require_once("simple_html_dom.php");
$pageContent = file_get_html("http://example.com");
foreach ($pageContent->find("link") as $link){
    `enter code here`echo $link->href . "<br>";
}
  1. 使用DOMDocument

在我的例子中,我将使用一个快速的“file_get_contents”来完成工作。 您可能想要发出正确的 CURL 请求。

$html = file_get_contents('http://www.exmaple.com');
$doc = new DOMDocument();
$doc->loadHTML($html);
$xp = new DOMXPath($doc);

$res = $xp->query('//link');
if($res->length > 0){
    foreach ($res as $node){
        echo $node -> nodeValue;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM