无法解析成标签 - PHP - 简单的html dom

无法解析成<code>标签 -  PHP  - 简单的html dom

问题描述:

I am trying to extract the content of a <div> nested inside a <code> tag with PHP Simple HTML DOM Parser but I am always getting the error Trying to get property of non-object in... as if the parser was finding nothing inside my <div>

The code I'm using is

include_once('simplehtmldom_1_5/simple_html_dom.php');

// Create a DOM object
$html = new simple_html_dom();

// Load HTML
$html->load('<code><div>hello</div></code>');

// Extract div content
echo $html->find('div',0)->innertext;

But if instead of using <code><div>hello</div></code> as my sample code i use <span><div>hello</div></span> it works... it seems like I'm having problems only looking inside the code tag.

What's wrong with what i'm doing? Hope you guys can point me in the right direction, thank you very much for your support!

simplehtmldom among others strips out pre formatted tags. If you want code tag to be recognized delete or comment out line 1076 in *simple_html_dom.php*

According to the source code for Simple HTML DOM it automagically removes code tags when it loads the HTML into the parser.

If you need the functionality you'll need to remove the reference to remove_noise() in the load() function within simplehtmldom.php.

This should produce the results you expect, but obviously may well introduce other issues, depending on the authors reasoning for removing the tags in the first place.