I have an HTML page in a string variable. I need to look inside the h2 tags of the whole document if the word "Blog" exists. I'm using pregmatch but I can not create the expression to find.
Can you help me one more time?
Thank you!
I have an HTML page in a string variable. I need to look inside the h2 tags of the whole document if the word "Blog" exists. I'm using pregmatch but I can not create the expression to find.
Can you help me one more time?
Thank you!
You can use DOMDocument
to filter by H2
.
You did not enter the HTML, so imagine this:
<div id="content">
<h2>Blog</h2>
<p class="y">Isto é um blog</p>
<h2>Index</h2>
<p class="x">Isto é um Index</p>
</div>
In PHP use something similar to this:
$seu_html = '<div id="content"> <h2>Blog</h2> <p class="y">Isto é um blog</p><h2>Index</h2> <p class="x">Isto é um Index</p></div>';
$doc = new DOMDocument();
$doc->loadHTML($seu_html);
// Todos os H2
$h2 = $doc->getElementsByTagName("h2");
// Loop de todos os H2
foreach($h2 as $item){
// Echo se encontrar a palavra Blog
echo strpos(strtolower($item->textContent), 'blog') !== false ? 'Achamos um H2 blog' : '';
}
You can try this clicking here.
The documentation for DOMDocument
is extremely broad (for me I believe not very friendly!) and has many functions, maybe even some that removes the need for foreach
! See this clicking here and here .
IMPORTANT: The
strpos
was used so that it can "contain" but not necessarily be "equal" or "identical". For this reason, ifH2
is "Blog do Zé", it will be VALID!strtolower
was used to make text smaller, so removing the "Blog" error is different from "blog", for example.
Use /<h2>(.*?)<\/h2>/
, for example:
$seu_html = '<div id="content"> <h2>Blog</h2> <p class="y">Isto é um blog</p><h2>Index</h2> <p class="x">Isto é um Index</p></div>';
preg_match_all ("/<h2>(.*?)<\/h2>/", $seu_html, $h2);
// Loop de todos os H2
foreach($h2[1] as $item){
// Echo se encontrar a palavra Blog
echo strpos(strtolower($item), 'blog') !== false ? 'Achamos um H2 blog' : '';
}
You can test this clicking here.