From 7fba8a2cb7bc4b31eef1b83db4ad57e664d6dbf7 Mon Sep 17 00:00:00 2001 From: Michael Query Date: Fri, 23 Oct 2015 14:48:32 -0600 Subject: [PATCH 1/2] Functionality get node indices and delete nodes Get a nodes index or the last child node index from a node object. --- .../simplehtmldom_1_5/simple_html_dom.php | 49 ++++++++++++++++++- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/Src/Sunra/PhpSimple/simplehtmldom_1_5/simple_html_dom.php b/Src/Sunra/PhpSimple/simplehtmldom_1_5/simple_html_dom.php index 2024b66..4019117 100644 --- a/Src/Sunra/PhpSimple/simplehtmldom_1_5/simple_html_dom.php +++ b/Src/Sunra/PhpSimple/simplehtmldom_1_5/simple_html_dom.php @@ -116,7 +116,6 @@ class simple_html_dom_node public $nodetype = HDOM_TYPE_TEXT; public $tag = 'text'; public $attr = array(); - /** @var simple_html_dom_node[] $children */ public $children = array(); public $nodes = array(); public $parent = null; @@ -150,6 +149,26 @@ function clear() $this->children = null; } + /** + * Get this node's starting (self) index in the html tree + * @return mixed int + */ + function getSelfIndex(){ + return $this->_[HDOM_INFO_BEGIN]; + } + + /** + * Get the index of the last child belonging to this node + * @return mixed + */ + function getLastChildIndex(){ + if (isset($this->_[HDOM_INFO_END])) { + return $this->_[HDOM_INFO_END]; + }else { + return $this->getSelfIndex(); + } + } + // dump node's tree function dump($show_attr=true, $deep=0) { @@ -355,8 +374,9 @@ function innertext() if (isset($this->_[HDOM_INFO_TEXT])) return $this->dom->restore_noise($this->_[HDOM_INFO_TEXT]); $ret = ''; - foreach ($this->nodes as $n) + foreach ($this->nodes as $n) { $ret .= $n->outertext(); + } return $ret; } @@ -1038,6 +1058,31 @@ function __destruct() $this->clear(); } + /** + * Delete a node and its children recursively + * + * @param $nodes array Node to be deleted + */ + function deleteNodes($nodes) + { + foreach ($nodes as $n) { + $n->outertext = ""; + } + $this->load($this->save(), true, false); + } + + /** + * @param $selector string + */ + function deleteNodeBySelector($selector){ + foreach ($this->find($selector) as $node) + { + $node->outertext = ''; + } + + $this->load($this->save()); + } + // load html from string function load($str, $lowercase=true, $stripRN=true, $defaultBRText=DEFAULT_BR_TEXT, $defaultSpanText=DEFAULT_SPAN_TEXT) { From 3a7b1694ad8172ad701e9539029e37abded65120 Mon Sep 17 00:00:00 2001 From: Michael Query Date: Fri, 23 Oct 2015 15:23:39 -0600 Subject: [PATCH 2/2] Update composer.json --- composer.json | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/composer.json b/composer.json index 6d21ae1..e5ade8c 100644 --- a/composer.json +++ b/composer.json @@ -1,18 +1,18 @@ { - "name": "sunra/php-simple-html-dom-parser", + "name": "michaelquery/php-simple-html-dom-parser", "type": "library", "description": "Composer adaptation of: A HTML DOM parser written in PHP5+ let you manipulate HTML in a very easy way! Require PHP 5+. Supports invalid HTML. Find tags on an HTML page with selectors just like jQuery. Extract contents from HTML in a single line.", "keywords": ["html", "dom", "parser"], - "homepage": "https://github.com/sunra/php-simple-html-dom-parser", + "homepage": "https://github.com/michaelquery/php-simple-html-dom-parser", "license": "MIT", "authors": [ { "homepage": "http://sourceforge.net/projects/simplehtmldom/" }, { - "name": "Sunra", - "email": "sunra@yandex.ru", - "homepage": "https://github.com/sunra" + "name": "Michael Query", + "email": "michaelquery@gmail.com", + "homepage": "https://github.com/michaelquery" } ], "require": { @@ -21,4 +21,4 @@ "autoload": { "psr-0": { "Sunra\\PhpSimple\\HtmlDomParser": "Src/" } } -} \ No newline at end of file +}