如何在数据库列的可点击标题之间分别标题?

I have a database column which has content with h3 headings split by paragraph content and then in my PHP file I am replacing and exploding these to create the clickable headings, see code below.

<dl class="accordion">
<?php
$accordion = stripslashes($row->barrNotable);
$accordion = str_replace(
array("<h3>","</h3>"),
array("<dt class=\"title\"><p>", "</p></dt><dd>"), $accordion);
$accordion = preg_replace('</dd>', '', $accordion, 1);
$accordion = str_replace('<>','', $accordion);
$pieces = explode("</dd>", $accordion);
$acc1 = $pieces[0] . "
</dd>
";
$acc2 = $pieces[1] . "
</dd>
";
$acc3 = $pieces[2] . "
</dd>
";
$acc4 = $pieces[3] . "
</dd>
";
echo $acc1;
?>
</dl>

This all works absolutely fine, but how do I include h2 headings in between to separate the accordions, so for example what I would like is a h2 heading which has multiple h3 headings that are clickable and then the accordion end and then it shows the next h2 heading and so on.

I have tried placing the <dl class="accordion"> inside the array but this doesn't work and just create clickable heading with in another heading.