【问题标题】:In php, how to get all link tags from header? [closed]在php中,如何从标题中获取所有链接标签? [关闭]
【发布时间】:2022-10-07 23:13:25
【问题描述】:

我们都在网页的标题中看到/使用过这样的内容:

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

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

就像

get_meta_tags(url);

检索元标记。

【问题讨论】:

    标签: php html tags


    【解决方案1】:

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

    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;
          }
      }
      

    【讨论】:

    • 我认为有一种更简单的方法可以做到这一点。谢谢你。
    猜你喜欢
    • 1970-01-01
    • 2015-09-10
    • 1970-01-01
    • 2014-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 2012-07-09
    相关资源
    最近更新 更多