]> _ Git - cubist_util.git/commitdiff
wip #7048 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 21 Aug 2024 15:48:28 +0000 (17:48 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 21 Aug 2024 15:48:28 +0000 (17:48 +0200)
src/Html.php

index d6bc1490d6e7b1cf2f5a2050c08507cde83fc9ed..c14532f410157194079076558b41e3b25f9af25f 100644 (file)
@@ -34,7 +34,11 @@ class Html
     protected static function _getTextNode(DOMNode $domNode): array
     {
         $res = [];
+        $exclude = ['script', 'style', 'link'];
         foreach ($domNode->childNodes as $node) {
+            if (in_array($node->nodeName, $exclude)) {
+                continue;
+            }
             if ($node instanceof \DOMText) {
                 $res[] = $node->nodeValue;
             }
@@ -54,7 +58,15 @@ class Html
             $doc = new DOMDocument('1.0', 'utf-8');
             $doc->loadHTML('<?xml version="1.0" encoding="utf-8">' . $string);
 
-            $res = static::_getTextNode($doc);
+            $nodes = static::_getTextNode($doc);
+            $res = [];
+            foreach ($nodes as $node) {
+                $node = trim($node);
+                if (!$node) {
+                    continue;
+                }
+                $res[] = $node;
+            }
             return $res;
         } catch (\Exception $e) {
             return [$string];