PHP: Convert
  • Tree HTML tag to an array

后端 未结 4 977
无人共我
无人共我 2021-01-16 10:53

I\'m using jsTree and I need to convert this HTML tag tree code

  • to a PHP array. The jsTree HTML tag will be passed to PHP to be parsed and
4条回答
  •  天涯浪人
    2021-01-16 11:37

    If you have a UL that does not contain any href elements (e.g. just plain text nexted ul).. I've adjusted Salman's simplehtmldom code to work with that:

    $DOM = str_get_html( $catshtml );
    $ARR = array( );
    function WalkUL( $ul, &$ar )
    {
        foreach( $ul->children as $li )
        {
            if ( $li->tag != "li" )
            {
                continue;
            }
            $arar = array( );
            foreach( $li->children as $ulul )
            {
                if ( $ulul->tag != "ul" )
                {
                    continue;
                }
                WalkUL( $ulul, $arar );
            }
            $ar[ trim(preg_replace("#]*>(.+]*>|)#is","",$li->innertext)) ] = $arar;
        }
    }
    WalkUL( $DOM->find( "ul", 0 ), $ARR );
    echo "
    "; print_r( $ARR );
    

提交回复
热议问题