在WordPress网站上,如何使用PHP条件检查是否存在任何HTML元素(带ID或类)?
无论如何,我已尝试使用此代码:
$new = $html->find("#banner_id");
if($new){
echo "Exists";
} else {
echo "Not Exists";
}
但是这导致了以下错误:
答案 0 :(得分:1)
I found your $html variable is null you need to create object before start to find the element by id
<section id='yha-tools'>
<div class='close'>+</div>
<div class='close'>+</div>
<div class='close'>+</div>
<div class='close'>+</div>
<div class='close'>+</div>
<div class='close'>+</div>
</section>
<output>6</output>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Either use any method from above list then start to find it.
$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
Complete code
$main = $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/