0

I am trying to figure out how to use Regex in PHP to backtrack and find a </div> tag preceding some specific html.

Here is the full HTML I want to look at including the closing tag I want to find and ultimately remove from the string:

</div>
        <div class="dHeading">Grades

It actually has a new line and spaces in between the 2 tags. So I want to target that </div> tag and remove it from the string. Any help would be appreciated.

I am using an online regex tester. Here is what I have right now.

Regex:

/(</div>)([\w\W]*?)(<div)/
dmikester1
  • 1,374
  • 11
  • 55
  • 113

1 Answers1

2

I suspect if this question is about parsing HTML document at all and regex is totally fine to deal with it:

preg_replace('~</div>\s+(?=<div)~', '', $content);

will do the job. You needed a positive lookahead and considering whitespaces in middle of closing and opening tags, precisely.

revo
  • 47,783
  • 14
  • 74
  • 117