I'm trying match nested <ul>s and their contents. I'm able to match single uls using this pattern:
/<ul(.+)>(.+)<\/ul>/Us
But when I have nested uls, the pattern catches all the starting <ul>s but finishes at the first closing </ul>.
What I need is a way to tell it "while you're doing this, count up the number of opening <ul>s and make sure to include the same number of closing </ul>s before ending the pattern match". Basically, make it more greedy but not so greedy as to grab the entire source string. I need to make it "greedy times the number of <ul>s in the current match".
I tried:
/<ul(.+)>(.+)(<\/ul>){1,2}/Us (since there are usually just 1 or 2 levels of uls) but that didn't catch the second </ul>
This is part of a preg_replace that wraps these in <p> tags for further processing, then strips the <p> tags. The "whys" of that are a bit complicated - it has to do with placing images in numbered paragraphs or numbered uls/ols. But the whole preg_replace looks like this:
$pattern = array("/<ul(.+)>(.+)<\/ul>/Us", "/<ol(.+)>(.+)<\/ol>/Us", "/<h2>(.+)<\/h2>/Us", "/<h3>(.+)<\/h3>/Us", "/<h4>(.+)<\/h4>/Us", "/<table>(.+)<\/table>/Us");
$replace = array("<p><ul$1>$2</ul></p>", "<p><ol$1>$2</ol></p>", "<p><h2>$1</h2></p>", "<p><h3>$1</h3></p>", "<p><h4>$1</h4></p>", "<p><table>$1</table></p>");
$text = preg_replace($pattern, $replace, $text);
Any brilliant regex minds have a tip for me?