Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding and Echoing out a Specific ID from HTML document with PHP

Tags:

php

I am grabbing the contents from google with PhP, how can I search $page for elements with the id of "#lga" and echo out another property? Say #lga is an image, how would I echo out it's source?

No, i'm not going to do this with Google, Google is strictly an example and testing page.

<body><img id="lga" src="snail.png" /></body>

I want to find the element named "lga" and echo out it's source; so the above code I would want to echo out "snail.png".

This is what i'm using and how i'm storing what I found:

<?php
$url = "https://www.google.com/";
$page = file($url);

foreach($page as $part){
}
?>
like image 731
Cody Nelson Avatar asked Apr 24 '26 06:04

Cody Nelson


1 Answers

You can achieve this using the built-in DOMDocument class. This class allows you to work with HTML in a structured manner rather than parsing plain text yourself, and it's quite versatile:

$dom = new DOMDocument();
$dom->loadHTML($html);

To get the src attribute of the element with the id lga, you could simply use:

$imageSrc = $dom->getElementById('lga')->getAttribute('src');

Note that DOMDocument::loadHTML will generate warnings when it encounters invalid HTML. The method's doc page has a few notes on how to suppress these warnings.

Also, if you have control over the website you are parsing the HTML from, it might be more appropriate to have a dedicated script to serve the information you are after. Unless you need to parse exactly what's on a page as it is served, extracting data from HTML like this could be quite wasteful.

like image 169
Stecman Avatar answered Apr 25 '26 21:04

Stecman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!