The following solution works if you can live with whitespace being
altered between <LI> tags:
$input = preg_replace("/(<\/li>)\s*(<li>)/i","$1$2",$input);
$input = preg_replace("/(?<!li>)<li>/i","<UL><LI>",$input);
$input = preg_replace("/<\/li>(?!<li>)/i","</LI></UL>",$input);
Step 1 closes the whitespace between <LI> tags
Step 2 matches any <LI> tag which is not preceeded by a closing </LI>
tag and inserts a start <UL> through replacement.
Step 3 does the reverse, adding closing <UL> tags.
Running the above creates the string:
<P ALIGN="LEFT">this is some text<br/><br/>
<h2>Stay Safe</h2><br/>
<B>Residential care</B><br/><br/>this is some text<br/></P>
<UL><LI>list item 1</LI><LI>list item 2</LI><LI>list item
3</LI><LI></LI></UL><P ALIGN="LEFT">Our staff are blah blah</P><UL><LI>list
item 1</LI><LI>list item 2</LI><LI>list item 3</LI></UL>some more text
might start straight away
Which matches your test case (but removes whitespace between LI). Let
me know if I can improve this solution for you. |