From the course: PHP Tips, Tricks, and Techniques

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

Finding all links in a webpage

Finding all links in a webpage

- [David] Hi, I'm David Powers, and welcome to this week's edition of PHP Tips, Tricks, and Techniques designed to help you become a smarter, more productive PHP developer. This week I'm going to show you how to find all the links in a webpage. This can be useful for building an index of links or for testing. In the exercise files for this video, I've included this webpage adapted from one of my other courses, so I'll be working with a local file, but the same technique works equally well with online pages. So let's start coding. We're going to use the document object model, or DOM to inspect the HTML and extract the links. Now the great thing about how PHP handles this is that it will do it's best to parse the HTML page even if it's got mistakes in it. But to avoid having errors displayed in the results, it's a good idea to suppress them, and we do that using libxml, use internal errors. And we parse that true as an argument. Under the hood, PHP is using XML to parse the DOM, and…

Contents