Skip to content

Commit d5ee248

Browse files
added $returnAsArray option to HtmlNode::text() method
1 parent 72f8510 commit d5ee248

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

src/PHPHtmlParser/Dom/HtmlNode.php

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace PHPHtmlParser\Dom;
34

45
use PHPHtmlParser\Exceptions\UnknownChildTypeException;
@@ -46,7 +47,7 @@ class HtmlNode extends InnerNode
4647
*/
4748
public function __construct($tag)
4849
{
49-
if ( ! $tag instanceof Tag) {
50+
if (!$tag instanceof Tag) {
5051
$tag = new Tag($tag);
5152
}
5253
$this->tag = $tag;
@@ -61,27 +62,27 @@ public function __construct($tag)
6162
*/
6263
public function innerHtml()
6364
{
64-
if ( ! $this->hasChildren()) {
65+
if (!$this->hasChildren()) {
6566
// no children
6667
return '';
6768
}
6869

69-
if ( ! is_null($this->innerHtml)) {
70+
if (!is_null($this->innerHtml)) {
7071
// we already know the result.
7172
return $this->innerHtml;
7273
}
7374

74-
$child = $this->firstChild();
75+
$child = $this->firstChild();
7576
$string = '';
7677

7778
// continue to loop until we are out of children
78-
while ( ! is_null($child)) {
79+
while (!is_null($child)) {
7980
if ($child instanceof TextNode) {
8081
$string .= $child->text();
8182
} elseif ($child instanceof HtmlNode) {
8283
$string .= $child->outerHtml();
8384
} else {
84-
throw new UnknownChildTypeException('Unknown child type "'.get_class($child).'" found in node');
85+
throw new UnknownChildTypeException('Unknown child type "' . get_class($child) . '" found in node');
8586
}
8687

8788
try {
@@ -101,8 +102,8 @@ public function innerHtml()
101102
/**
102103
* Gets the html of this node, including it's own
103104
* tag.
104-
*
105105
* @return string
106+
* @throws UnknownChildTypeException
106107
*/
107108
public function outerHtml()
108109
{
@@ -111,7 +112,7 @@ public function outerHtml()
111112
return $this->innerHtml();
112113
}
113114

114-
if ( ! is_null($this->outerHtml)) {
115+
if (!is_null($this->outerHtml)) {
115116
// we already know the results.
116117
return $this->outerHtml;
117118
}
@@ -136,34 +137,39 @@ public function outerHtml()
136137

137138
/**
138139
* Gets the text of this node (if there is any text). Or get all the text
139-
* in this node, including children.
140+
* in this node, including children
140141
*
141142
* @param bool $lookInChildren
143+
* @param bool $returnAsArray
142144
* @return string
143145
*/
144-
public function text($lookInChildren = false)
146+
public function text($lookInChildren = false, $returnAsArray = false)
145147
{
146148
if ($lookInChildren) {
147-
if ( ! is_null($this->textWithChildren)) {
149+
if (!is_null($this->textWithChildren)) {
148150
// we already know the results.
149151
return $this->textWithChildren;
150152
}
151-
} elseif ( ! is_null($this->text)) {
153+
} elseif (!is_null($this->text)) {
152154
// we already know the results.
153155
return $this->text;
154156
}
155157

156158
// find out if this node has any text children
157-
$text = '';
159+
$text = $returnAsArray ? [] : "";
158160
foreach ($this->children as $child) {
159161
/** @var AbstractNode $node */
160162
$node = $child['node'];
161163
if ($node instanceof TextNode) {
162-
$text .= $child['node']->text;
164+
if ($returnAsArray) array_push($text,
165+
$child['node']->text);
166+
else $text .= $child['node']->text;
163167
} elseif ($lookInChildren &&
164168
$node instanceof HtmlNode
165169
) {
166-
$text .= $node->text($lookInChildren);
170+
if ($returnAsArray) array_push($text,
171+
$node->text($lookInChildren, $returnAsArray));
172+
else $text .= $node->text($lookInChildren);
167173
}
168174
}
169175

@@ -185,7 +191,7 @@ protected function clear()
185191
{
186192
$this->innerHtml = null;
187193
$this->outerHtml = null;
188-
$this->text = null;
194+
$this->text = null;
189195

190196
if (is_null($this->parent) === false) {
191197
$this->parent->clear();

0 commit comments

Comments
 (0)