Wrap similar tags

0

[1] This is an example of what I would like to do, however, I do not know how to apply to my case. I would like to wrap items of the same type in a ol of it.

Original text:

<li>
  <p>a) texto longo;</p>
</li>
<li>
  <p>b)  texto longo;</p>
</li>
<li>
  <p>I - texto longo;</p>
</li>
<li>
  <p>a) texto longo;</p>
</li>
<li>
  <p>b) texto longo;</p>
</li>
<li>
  <p>c)  texto longo;</p>
</li>

I would like to return this value:

<ol>
   <li>
     <p>a) texto longo;</p>
   </li>
   <li>
     <p>b)  texto longo;</p>
   </li>
</ol>
<li>
  <p>b)  texto longo;</p>
</li>
<ol>
   <li>
     <p>a) texto longo;</p>
   </li>
   <li>
     <p>b)  texto longo;</p>
   </li>
   <li>
     <p>c)  texto longo;</p>
   </li>
</ol>

[1] = link

    
asked by anonymous 03.05.2015 / 19:41

1 answer

1

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

    
04.05.2015 / 03:41