[Solved] How do I obtain the canonical value using PHP DomDocument?


There are multiple ways to do this.

Using XML:

<?php

$html = "<link rel="canonical" href="http://test.com/asdfsdf/sdf/" />";

$xml  = simplexml_load_string($html);
$attr = $xml->attributes();
print_r($attr);

?>

which outputs:

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [rel] => canonical
            [href] => http://test.com/asdfsdf/sdf/
        )

)

or, using Dom:

<?php

$html = "<link rel="canonical" href="http://test.com/asdfsdf/sdf/" />";

$dom = new DOMDocument;
$dom->loadHTML($html);
$nodes = $dom->getElementsByTagName('link');
foreach ($nodes as $node)
{
    if ($node->getAttribute('rel') === 'canonical')
    {
        echo($node->getAttribute('href'));
    }
}

?>

which outputs:

http://test.com/asdfsdf/sdf/

In both examples, more code is required if you’re parsing an entire HTML file, but they demonstrate most of the structure that you’ll need.

Code modified from this answer and the documentation on Dom.

3

solved How do I obtain the canonical value using PHP DomDocument?