3v4l.org

run code in 300+ PHP versions simultaneously
<?php function findWord(String $html, String $searchWord): Bool { $dom = new DOMDocument; libxml_use_internal_errors(true); $dom->loadHTML($html); $htmlContent = $dom->getElementsByTagName('body')->item(0); $text = $htmlContent->textContent; return strpos($text, $searchWord) !== false; } // First let's start with html we know the word exists $html = <<<'HTML' <html> <head> <meta content="illustrative productions"> <script> var illustrative = true; </script> <style> .illustrative { background-color: #fff; } </style> </head> <body> <h1 class="illustrative">Hello World</h1> <p>Your search word appears here. <strong>illustrative</strong></p> </body> </html> HTML; // Gives us "Found" if (findWord($html, "illustrative")) { echo "Found"; } else { echo "Not Found"; } // Now let's try HTML we know the word doesn't exist $html = <<<'HTML' <html> <head> <meta content="illustrative productions"> <script> var illustrative = true; </script> <style> .illustrative { background-color: #fff; } </style> </head> <body> <h1 class="illustrative">Hello World</h1> <p>Your search word never appears here.</p> </body> </html> HTML; // Gives us "Not Found" if (findWord($html, "illustrative")) { echo "Found"; } else { echo "Not Found"; }

preferences:
130.82 ms | 405 KiB | 5 Q