Extract All URL’s from a Web Page using PHP

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.
URLs From '.$web_page_URL.''; echo $list_of_URL; } ?>

PHP Source Code

This source code used to extract all URL's type from the user. To show all the list of single URL. loadHTML($urlContent); $xpath = new DOMXPath($dom); $hrefs = $xpath->evaluate("/html/body//a"); for($i = 0; $i length; $i++){ $href = $hrefs->item($i); $url = $href->getAttribute('href'); $url = filter_var($url, FILTER_SANITIZE_URL); // validate url if(!filter_var($url, FILTER_VALIDATE_URL) === false){ $list_of_URL .= '
  • '.$url.'
  • '; } } $list_of_URL = '
      '.$list_of_URL.'
    '; }else{ $list_of_URL = '

    The URLs not found. Type a new one.

    '; } } ?>

    Output

    Result Hope that this simple yet useful tutorials that I created may help you to your future projects. If you are interested in programming, we have an example of programs that may help you even just in small ways. Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.

    Add new comment