Skip to content
Advertisement

Web scraping in PHP

I’m looking for a way to make a small preview of another page from a URL given by the user in PHP.

I’d like to retrieve only the title of the page, an image (like the logo of the website) and a bit of text or a description if it’s available. Is there any simple way to do this without any external libraries/classes? Thanks

So far I’ve tried using the DOCDocument class, loading the HTML and displaying it on the screen, but I don’t think that’s the proper way to do it

Advertisement

Answer

I recommend you consider simple_html_dom for this. It will make it very easy.

Here is a working example of how to pull the title, and first image.

JavaScript

Here is a second example that will do the same without an external library. I should note that using regex on HTML is NOT a good idea.

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement