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

Time:09-22

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,

CodePudding user response:

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