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 –
- How To Generate Fake Data in PHP Using Faker Library
- How to Generate Fake Image URLs in PHP Using Faker
- How to Generate Random Name of Person in PHP
- How to Get HTML Tag Value in PHP | DOMDocument Object
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.
Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.
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.