简体   繁体   中英

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

We all seen/used something like this in the webpage's header:

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

How to get all links tags from header using php?

Just like

get_meta_tags(url);

Which retrieves meta tags.

Thank you,

There are multiple ways to crawl a page and parse through it.

  1. You could use 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. Using DOMDocument

In my exmple, i'll use a quick "file_get_contents" to get the job done. You probably want to make a proper CURL request.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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