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…

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

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"

The cleanest way to do it without breaking forwards compatibility would be to create your own widget, extending the widget class you want to fix. Strip it back to the bare essentials of constructor and the widget method (in which you fix the HTML), then attach a function to widgets_init after the original widget which registers your widget and deregisters the original.