-1

We have index.php with code:

<div class="test"> text text text text</div>
<div class="test2"> text text text text</div>
<div class="test3"> text text text text</div>
<div class="test4"> text text text text</div>

In file test.php we use code:

ob_start(); // start output buffer
include 'index.php';
$template = ob_get_contents(); // get contents of buffer
ob_end_clean();
return $template;

Tell me please how remove div with class test2 and all content in him from html which we get in $template?

P.S.: we want remove div with class test2 regardless of the attributes inside the div tag.

1 Answers1

1

Okay, you have a html structure in a variable. You can remove this with a preg_replace.

$template = preg_replace('/<div.*?class="test2".*?>.*?</div>/','', $template);

Another way is to use a DOM parser.

Adrian B
  • 1,490
  • 1
  • 19
  • 31
  • but how been if div have more attributes? we would be remove div with clss `test2` regardless of the attributes inside the div tag. –  May 29 '14 at 19:50
  • I will edit this for situation with many attributes – Adrian B May 29 '14 at 19:53