Find content within two html tags - Regex

Asked

Viewed 1,470 times

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 answer

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 the H2 for "Blog do Zé", will be VALID! O strtolower was used to make the text minute, thus removing the error of "Blog" being different from "blog", for example.

If you still want to use REGEX....

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

You are not signed in. Login or sign up in order to post.