Find and Extract All links From a HTML String in PHP

Share this Article
Reading Time: 5 minutes
501 Views

Inside this article we will see the concept of find and extract all links from a HTML string in php. Concept of this article will provide very classified information to understand the things.

This PHP tutorial is based on how to extract all links and their anchor text from a HTML string. In this guide, we will see how to fetch the HTML content of a web page by URL and then extract the links from it. To do this, we will be use PHP’s DOMDocument class.

DOMDocument of PHP also termed as PHP DOM Parser. We will see step by step concept to find and extract all links from a html using DOM parser.

Learn More –

Let’s get started.


Example 1: Get All Links From HTML String Value

Inside this example we will consider a HTML string value. From that html value we will extract all links.

Create file index.php inside your application.

Open index.php and write this complete code into it.

<?php 

// HTML String
$htmlString = "<html>
  <head></head>
  <body>
    <a href='https://www.google.com/' title='Google URL'>Google</a>
    <a href='https://www.youtube.com/' title='Youtube URL'>Youtube</a>
    <a href='https://onlinewebtutorblog.com/' title='Website URL'>Online Web Tutor</a>
  </body>
</html>";

//Create a new DOMDocument object.
$htmlDom = new DOMDocument;

//Load the HTML string into our DOMDocument object.
@$htmlDom->loadHTML($htmlString);

//Extract all anchor elements / tags from the HTML.
$anchorTags = $htmlDom->getElementsByTagName('a');

//Create an array to add extracted images to.
$extractedAnchors = array();

//Loop through the anchors tags that DOMDocument found.
foreach($anchorTags as $anchorTag){

    //Get the href attribute of the anchor.
    $aHref = $anchorTag->getAttribute('href');

    //Get the title text of the anchor, if it exists.
    $aTitle = $anchorTag->getAttribute('title');

    //Add the anchor details to $extractedAnchors array.
    $extractedAnchors[] = array(
        'href' => $aHref,
        'title' => $aTitle
    );
}

echo "<pre>";
//print_r our array of anchors.
print_r($extractedAnchors);

Concept

Output

When we run index.php. Here is the output


Example 2: Get All Links From a Web Page

Inside this example we will use web page URL to get all links.

Create file index.php inside your application.

Open index.php and write this complete code into it.

<?php 

$htmlString = file_get_contents('https://onlinewebtutorblog.com/');

//Create a new DOMDocument object.
$htmlDom = new DOMDocument;

//Load the HTML string into our DOMDocument object.
@$htmlDom->loadHTML($htmlString);

//Extract all anchor elements / tags from the HTML.
$anchorTags = $htmlDom->getElementsByTagName('a');

//Create an array to add extracted images to.
$extractedAnchors = array();

//Loop through the anchors tags that DOMDocument found.
foreach($anchorTags as $anchorTag){

    //Get the href attribute of the anchor.
    $aHref = $anchorTag->getAttribute('href');

    //Get the title text of the anchor, if it exists.
    $aTitle = $anchorTag->getAttribute('title');

    //Add the anchor details to $extractedAnchors array.
    $extractedAnchors[] = array(
        'href' => $aHref,
        'title' => $aTitle
    );
}

echo "<pre>";
//print_r our array of anchors.
print_r($extractedAnchors);

Output

When we run index.php. Here is the output

We hope this article helped you to Find and Extract All links From a HTML String in PHP Tutorial in a very detailed way.

Buy Me a Coffee

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Read More Articles