Search for content within two html tags - Regex

1

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!

    
asked by anonymous 19.08.2016 / 01:37

1 answer

2

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, if H2 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.

If you still want to use REGEX ....

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.

    
19.08.2016 / 02:30