phphtml-parsingsanitizationremovechild

Remove div element with specific class from HTML document


I am trying to remove following pattern from a string:

<div class="main_title">Content 1</div> 

where 'Content 1' may vary between strings.

The following does not seem to be working:

$output = preg_replace('<div class="main_title">.*</div>', " ", $output);

Am I missing something obvious?


Solution

  • As others says in the comments, don't use regular expressions to parse HTML, use SimpleXML or DOMDocument instead. If you need a regex yet, you need to put the pattern delimiters in your code:

    $output = preg_replace('#<div class="main_title">.*</div>#', " ", $output);