Extract All URL’s from a Web Page using PHP
Submitted by alpha_luna on Friday, August 19, 2016 - 12:21.
Extract All URL’s from a Web Page using PHP
In this tutorial, we are going to learn how to Extract All URL’s from a Web Page using PHP. This tutorial, we are going to extract all URL’s in your web page and it’s familiar in many cases. Type one URL to the text box and we are going to extract it. Then, it can easily get all URL’s from a web page using PHP. We have short and simple source code in this tutorial to extract all URL’s from a web page using PHP. You can also check the live demo of this simple tutorial, so you can get an idea and you can try this out, let's start coding.Creating Markup
This simple markup where the user can type their desired URL's to extract all and the list of all URL's extract it by the user.- <div class="container">
- <form method="post">
- <input type="text" name="web_page_URL" value="<?php echo $web_page_URL; ?>" autofocus="autofocus" placeholder="Type your URL . . . . ."/>
- <input type="submit" name="submit" value="Extract URLs"/>
- </form>
- <?php if(!empty($list_of_URL)){
- echo '<h2 style="color:blue; font-family:cursive;">URLs From '.$web_page_URL.'</h2>';
- echo $list_of_URL;
- } ?>
- </div>
PHP Source Code
This source code used to extract all URL's type from the user. To show all the list of single URL.- <?php
- $list_of_URL = '';
- $web_page_URL = '';
- $web_page_URL = $_POST['web_page_URL'];
- $dom = new DOMDocument();
- @$dom->loadHTML($urlContent);
- $xpath = new DOMXPath($dom);
- $hrefs = $xpath->evaluate("/html/body//a");
- for($i = 0; $i < $hrefs->length; $i++){
- $href = $hrefs->item($i);
- $url = $href->getAttribute('href');
- // validate url
- $list_of_URL .= '<li><a href="'.$url.'" target="_blank">'.$url.'</a></li>';
- }
- }
- $list_of_URL = '<ul style="font-size:18px; font-family:cursive;">'.$list_of_URL.'</ul>';
- }else{
- $list_of_URL = '<p style="color:blue; font-size:20px;">The URLs not found. Type a new one.</p>';
- }
- }
- ?>
Output

Add new comment
- 305 views