Using preg_replace to clean widget output HTML

I am working on a wordpress site that uses a plugin / sidebar widget. The issue I am experiencing is that the widget generates HTML that is not valid, an example of this is below…

<p>
<div>I am item 1</div>
<div>I am item 2</div>
<div>I am item 3</div>
<div>I am item 4</div>
<p></p>

From my understanding there is two things wrong with this code, the first being that it uses divs (block level element) inside of a Paragraph. The second being that there is an extra P tag that is not closed at the end.

I have spoken to the widget developers and, even though it is an expensive and paid plugin, are not in any rush to fix the issue.

I am going to attempt to use preg_replace to clean up the code, I was wondering if anyone had any examples or tutorials to doing this and targetting just the code within the widget container?

I realise this might be a question better placed on stackoverflow but though I would try my luck here first 🙂

Solutions Collecting From Web of "Using preg_replace to clean widget output HTML"