// simple html dom parser
// -----------------------------------------------------------------------------
class simple_html_dom {
public $root = null;
public $nodes = array();
public $callback = null;
public $lowercase = false;
protected $pos;
protected $doc;
protected $char;
protected $size;
protected $cursor;
protected $parent;
protected $noise = array();
protected $token_blank = " \t\r\n";
protected $token_equal = ' =https://www.jb51.net/>';
protected $token_slash = " />\r\n\t";
protected $token_attr = ' >';
// use isset instead of in_array, performance boost about 30%...
protected $self_closing_tags = array('img'=>1, 'br'=>1, 'input'=>1, 'meta'=>1, 'link'=>1, 'hr'=>1, 'base'=>1, 'embed'=>1, 'spacer'=>1);
protected $block_tags = array('root'=>1, 'body'=>1, 'form'=>1, 'div'=>1, 'span'=>1, 'table'=>1);
protected $optional_closing_tags = array(
'tr'=>array('tr'=>1, 'td'=>1, 'th'=>1),
'th'=>array('th'=>1),
'td'=>array('td'=>1),
'li'=>array('li'=>1),
'dt'=>array('dt'=>1, 'dd'=>1),
'dd'=>array('dd'=>1, 'dt'=>1),
'dl'=>array('dd'=>1, 'dt'=>1),
'p'=>array('p'=>1),
'nobr'=>array('nobr'=>1),
);
function __construct($str=null) {
if ($str) {
if (preg_match("/^http:\/\//i",$str) || is_file($str))
$this->load_file($str);
else
$this->load($str);
}
}
function __destruct() {
$this->clear();
}
// load html from string
function load($str, $lowercase=true) {
// prepare
$this->prepare($str, $lowercase);
// strip out comments
$this->remove_noise("'<!--(.*?)-->'is");
// strip out cdata
$this->remove_noise("'<!\[CDATA\[(.*?)\]\]>'is", true);
// strip out <style> tags
$this->remove_noise("'<\s*style[^>]*[^/]>(.*?)<\s*/\s*style\s*>'is");
$this->remove_noise("'<\s*style\s*>(.*?)<\s*/\s*style\s*>'is");
// strip out <script> tags
$this->remove_noise("'<\s*script[^>]*[^/]>(.*?)<\s*/\s*script\s*>'is");
$this->remove_noise("'<\s*script\s*>(.*?)<\s*/\s*script\s*>'is");
// strip out preformatted tags
$this->remove_noise("'<\s*(?:code)[^>]*>(.*?)<\s*/\s*(?:code)\s*>'is");
// strip out server side scripts
$this->remove_noise("'(<\?)(.*?)(\?>)'s", true);
// strip smarty scripts
$this->remove_noise("'(\{\w)(.*?)(\})'s", true);
// parsing
while ($this->parse());
// end
$this->root->_[HDOM_INFO_END] = $this->cursor;
}
// load html from file
function load_file() {
$args = func_get_args();
$this->load(call_user_func_array('file_get_contents', $args), true);
}
// set callback function
function set_callback($function_name) {
$this->callback = $function_name;
}
// remove callback function
function remove_callback() {
$this->callback = null;
}