protected static function iterate_over_node($node)
{
if ($node instanceof \DOMText) {
return mb_ereg_replace("/[\\t\\n\\v\\f\\r ]+/im", " ", $node->wholeText);
}
if ($node instanceof \DOMDocumentType) {
// ignore
return "";
}
$nextName = self::next_child_name($node);
$prevName = self::prev_child_name($node);
$name = mb_strtolower($node->nodeName);
// start whitespace
switch ($name) {
case "hr":
return "------\n";
case "style":
case "head":
case "title":
case "meta":
case "script":
// ignore these tags
return "";
case "h1":
case "h2":
case "h3":
case "h4":
case "h5":
case "h6":
// add two newlines
$output = "\n";
break;
case "p":
case "div":
// add one line
$output = "\n";
break;
default:
// print out contents of unknown tags
$output = "";
break;
}
// debug
//$output .= "[$name,$nextName]";
if (isset($node->childNodes)) {
for ($i = 0; $i < $node->childNodes->length; $i++) {
$n = $node->childNodes->item($i);
$text = self::iterate_over_node($n);
$output .= $text;
}
}
// end whitespace
switch ($name) {
case "style":
case "head":
case "title":
case "meta":
case "script":
// ignore these tags
return "";
case "h1":
case "h2":
case "h3":
case "h4":
case "h5":
case "h6":
$output .= "\n";
break;
case "p":
case "br":
// add one line
if ($nextName != "div") {
$output .= "\n";
}
break;
case "div":
// add one line only if the next child isn't a div
if ($nextName != "div" && $nextName != null) {
$output .= "\n";
}
break;
case "a":
// links are returned in [text](link) format
$href = $node->getAttribute("href");
if ($href == null) {
// it doesn't link anywhere
if ($node->getAttribute("name") != null) {
$output = "[{$output}]";
}
} else {
if ($href == $output || $href == "mailto:{$output}" || $href == "http://{$output}" || $href == "https://{$output}") {
// link to the same address: just use link
$output;
} else {
// replace it
$output = "[{$output}]({$href})";
}
}
// does the next node require additional whitespace?
switch ($nextName) {
case "h1":
case "h2":
case "h3":
case "h4":
case "h5":
case "h6":
$output .= "\n";
break;
}
default:
// do nothing
}
return $output;
}