This can be done by separating the <li>
tags into a two-dimensional array and then merging them by adding the <ol>
tag when needed.
To separate them you can use the function preg_match_all
, first passing the regex, according to the text with the tags and third an empty variable to return what was found.
preg_match_all('/<li>\s*.*\s*<\/li>/', $documento, $matches);
The result is an array whose first item is another array with the list of texts that combine with the regex, the other items would be the texts that combine with the regex groups, but in this regex there are no groups. >
To identify the group, just pick up the first letter using the preg_replace
function, and check if it is the beginning of the letter or roman group, changing the type in use, and incrementing the group counter when it starts a different type .
$tipo = '';
$ultimo = '';
$count = 0;
$grupos = array();
foreach ($matches[0] as $k => $li) {
$letra = preg_replace('/\s*<li>\s*<p>\s*([a-zA-Z]).*<\/p>\s*<\/li>\s*/', '$1', $li);
switch ($letra) {
case 'a':
$tipo = 'letra';
break;
case 'I':
$tipo = 'romano';
break;
}
if ($k == 0) {
$grupos[$count][] = $li;
$ultimo = $tipo;
} else {
if ($tipo != $ultimo) {
$ultimo = $tipo;
$count++;
}
$grupos[$count][] = $li;
}
}
Finally, just add the <ol>
tag when there is more than one item in the group.
$html = '';
foreach ($grupos as $grupo) {
if (count($grupo) > 1) {
$html .= "<ol>\n".implode('', $grupo)."</ol>\n";
} else {
$html .= $grupo[0];
}
}
echo $html;
At the end we have the expected content in the variable $html