// save dom as string
function save($filepath='') {
$ret = $this->root->innertext();
if ($filepath!=='') file_put_contents($filepath, $ret);
return $ret;
}
// find dom node by css selector
function find($selector, $idx=null) {
return $this->root->find($selector, $idx);
}
// clean up memory due to php5 circular references memory leak...
function clear() {
foreach($this->nodes as $n) {$n->clear(); $n = null;}
if (isset($this->parent)) {$this->parent->clear(); unset($this->parent);}
if (isset($this->root)) {$this->root->clear(); unset($this->root);}
unset($this->doc);
unset($this->noise);
}
function dump($show_attr=true) {
$this->root->dump($show_attr);
}
// prepare HTML data and init everything
protected function prepare($str, $lowercase=true) {
$this->clear();
$this->doc = $str;
$this->pos = 0;
$this->cursor = 1;
$this->noise = array();
$this->nodes = array();
$this->lowercase = $lowercase;
$this->root = new simple_html_dom_node($this);
$this->root->tag = 'root';
$this->root->_[HDOM_INFO_BEGIN] = -1;
$this->root->nodetype = HDOM_TYPE_ROOT;
$this->parent = $this->root;
// set the length of content
$this->size = strlen($str);
if ($this->size>0) $this->char = $this->doc[0];
}
// parse html content
protected function parse() {
if (($s = $this->copy_until_char('<'))==='')
return $this->read_tag();
// text
$node = new simple_html_dom_node($this);
++$this->cursor;
$node->_[HDOM_INFO_TEXT] = $s;
$this->link_nodes($node, false);
return true;
}
// read tag info
protected function read_tag() {
if ($this->char!=='<') {
$this->root->_[HDOM_INFO_END] = $this->cursor;
return false;
}
$begin_tag_pos = $this->pos;
$this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next
// end tag
if ($this->char==='https://www.jb51.net/') {
$this->char = (++$this->pos<$this->size) ? $this->doc[$this->pos] : null; // next
$this->skip($this->token_blank_t);
$tag = $this->copy_until_char('>');