-1

I have a block of HTML that includes an unordered list of items, followed by a paragraph of text. HTML structure looks something like this:

<ul>
  <li>...</li>
</ul>
<p>...</p>

I need to split this block into separate vars. I was thinking of using explode but not sure if it's the best option.

$hpcs = explode("<\/ul>", $html);
$hlst = $hpcs[0];
$hsum = $hpcs[1];
ADyson
  • 57,178
  • 14
  • 51
  • 63
santa
  • 12,234
  • 49
  • 155
  • 255

1 Answers1

0

if you want to parse in you should use preg_replace

from https://gist.github.com/molotovbliss/18acc1522d3c23382757df2dbe6f0134

function ul_to_array ($ul) {
  if (is_string($ul)) {
    // encode ampersand appropiately to avoid parsing warnings
    $ul=preg_replace('/&(?!#?[a-z0-9]+;)/', '&amp;', $ul);
    if (!$ul = simplexml_load_string($ul)) {
      trigger_error("Syntax error in UL/LI structure");
      return FALSE;
    }
    return ul_to_array($ul);
  } else if (is_object($ul)) {
    $output = array();
    foreach ($ul->li as $li) {
      $output[] = (isset($li->ul)) ? ul_to_array($li->ul) : (string) $li;
    }
    return $output;
  } else return FALSE;
}
Daizygod
  • 32
  • 1
  • 3