user user -4 years ago 115
PHP Question

Simple HTML DOM Parser - Skip certain element

I am using the Simple HTML DOM Parser and I want to completely ignore the contents of the "nested" element and get the contents of the proceeding "pre" element.

<div id=parent>

<div class="nested">
<pre>Text that I want ignored</pre>
</div>

<pre>
This is the text I want to access
</pre>
</div>


I don't have control of the HTML source, and the owner has recently added the "nested" element. Before I accessed the content I needed by doing so:

$page_contents = file_get_html($url);
$div_content = $page_contents->find('div[id=parent]pre', 0)->innertext;


But obviously the new nested element has broken my method.

I can't seem to find any official documentation regarding this kind of scenario.

Answer Source

not tested but try this

$div_content = $page_contents->find('div[id=parent][class!=nested]pre', 0)->innertext;

or

$div_content = $page_contents->find('div[id=parent class!=nested]pre', 0)->innertext;

or maybe even just this I think this is really the one but again I have not tested

$div_content = $page_contents->find('div[class!=nested]pre', 1)->innertext;

still don't know if this will work but try this

$div_content = $page_contents->find('div[class!=nested pre]', 0)->innertext;

or

$div_content = $page_contents->find('div[class!=nested pre]', 0)->plaintext;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download