<?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";
}
- Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.26, 8.4.1 - 8.4.13
- FoundNot Found
preferences:
144.44 ms | 407 KiB | 5 Q