1
I have an HTML page in a string variable. I need to search inside the H2 tags of every document if there is the word "Blog". I’m using the pregmatch but I can’t create the expression to find.
You can help me one more time?
Thank you!
1
I have an HTML page in a string variable. I need to search inside the H2 tags of every document if there is the word "Blog". I’m using the pregmatch but I can’t create the expression to find.
You can help me one more time?
Thank you!
2
You can use the DOMDocument
to filter through H2
.
You didn’t report 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 test this clicking here.
The documentation of DOMDocument
is extremely broad (I believe that not very friendly!) and has many functions, perhaps even one that removes the need for foreach
! Look at 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 if theH2
for "Blog do Zé", will be VALID! Ostrtolower
was used to make the text minute, thus removing the error of "Blog" being different from "blog", for example.
Use the /<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.
Browser other questions tagged php regex
You are not signed in. Login or sign up in order to post.