Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8187463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:34:13+00:00 2026-06-07T02:34:13+00:00

<link rel=’canonical’ href=’http://test.com/asdfsdf/sdf/’ /> I need to get the canonical href value using Dom.

  • 0
<link rel='canonical' href='http://test.com/asdfsdf/sdf/' />

I need to get the canonical href value using Dom. How do I do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T02:34:14+00:00Added an answer on June 7, 2026 at 2:34 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to get the url from <link rel=prev title=Selected 3 href=http://mydomain.com/2.html /> in the
If we know that we can use var link = $('a[href=http://google.com]'); when we want
How can you retrieve the value of the rel attribute of a given link
Link: http://www.fraynepainting.com/services The problem is that div#dditem_2 (the div with the Take a look...
I have a link with a 'rel' attribute: <a href=# rel=music>Click</a> <p></p> I have
I have many link like this with different rel attributes <a href=# class=buy rel=WVSEU1Y>buy</a>
Here is the html: <html xmlns=http://www.w3.org/1999/xhtml> <head> <script src=http://code.jquery.com/jquery-1.4.4.js></script> <link rel = stylesheet type
I load css var ss = document.createElement(link); var url = 'http://site.ru/style.css'; ss.setAttribute(rel, stylesheet); ss.setAttribute(type,
I am using the following code to dynamically set a href link to open
From the following HTML snippet: <link rel=index href=/index.php /> <link rel=contents href=/getdata.php /> <link

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.