Involve similar tags

Asked

Viewed 62 times

0

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

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 you 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] = http://board.phpbuilder.com/showthread.php?10368819-Turning-a-array-into-HTML-nested-list

  • It is not clear what you want. Also, note that the resulting HTML displayed in your question is invalid.

  • I know the ol should stay inside the li to nestOls, but I tried to summarize to not stay long.

  • I have a list that has items that start with Roman numerals and others that start with a), b), c) and so on, I would like to wrap items of the same type in an ol from it

  • I edited your question including the comment above (and some other details), but it still doesn’t seem to be very clear what you want. In the first example is a), b), I), a), b), c), while in the second is (the result you want) a), b), b), a), b), c). The right way out wouldn’t be a), b), a), b), c) (only excluding the I)?

  • 1

    There’s a li left over there </ol>&#xA;<li>&#xA; <p>b) texto longo;</p>&#xA;</li>&#xA;<ol>

  • That read is from item "I"

Show 1 more comment

1 answer

1


This can be done by separating tags <li> in an array with two dimensions and then join them by adding the tag <ol> when necessary.

To separate them you can use the function preg_match_all, passing regex first, 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 text list that matches the regex, the other items would be the texts that match the regex groups, but in this regex there are no groups.

To identify the group just take the first letter using the function preg_replace, and check if it is the beginning of the group of letters or Romans, changing the type in use, and incrementing the group counter when initiating 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 tag <ol> 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.