Reputation: 109
On a WordPress website, how can I check the existence of any HTML element (with ID or class) using a PHP condition?
Anyway, I've tried with this code:
$new = $html->find("#banner_id");
if($new){
echo "Exists";
} else {
echo "Not Exists";
}
But this resulted in the following error:
Upvotes: 1
Views: 2774
Reputation: 2011
I found your $html variable is null you need to create object before start to find the element by id
$html = new simple_html_dom(); // Create a DOM object
Method - 1: $html->load_file('path-to-file/example.html'); // Load HTML from a HTML file
Method - 2: $html->load_file('http://www.yourdomainname.com/');// Load HTML from an URL
Method - 3: $html->load('<html><body><div id="banner_id">All the Besttttt!</div></body></html>'); // Load HTML from a string
Either use any method from above list then start to find it.
$main = $html->find('div[id=banner_id]',0);// Find the element where the id is equal to a particular value
Complete code
$html->load('<html><body><div id="banner_id">All the Besttttt!</div></body></html>');// Load HTML from a string
$divdata= $html->find('div[id=banner_id]',0);// Find the element where the id is equal to a particular value
For more detail please have a look here http://www.gurutechnolabs.com/php-simple-html-dom-parser-script/
Upvotes: 1