I have this string where I've put all opening tags into (array) $opened and all closing tags into (array) $closed, like so:
'<div>
Test
</div>
<div>
<blockquote>
<p>The quick</p>
<blockquote>
<p>brown fox <span>jumps <span>over <img src="#" /> the'
Results in these two arrays:
$opened =
array(8) {
[0]=> string(3) "div" // Need removed
[1]=> string(3) "div"
[2]=> string(10) "blockquote"
[3]=> string(1) "p" // Need removed
[4]=> string(10) "blockquote"
[5]=> string(1) "p"
[6]=> string(4) "span"
[7]=> string(4) "span"
}
$closed =
array(2) {
[0]=> string(3) "div"
[1]=> string(1) "p"
}
I need to somehow say:
Find the first occurrence of $closed[0] (which is "div") in the $opened array and remove it from the $opened array, then repeat until all $closed tags ("div and "p") have been removed from the top of $opened.
preg_split’s PREG_SPLIT_DELIM_CAPTURE). Then iterate the parts, put the opening tags on a stack and see if there is a corresponding closing tag and vice versa. If the opening and closing tags match, remove the opening tag from the stack. Doing so you can find mismatches of opening/closing tags and remove them or add the counterpart at the right position.