PHP: Convert
  • Tree HTML tag to an array

后端 未结 4 970
无人共我
无人共我 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:18

    Regarding:

    My problem I'm having a hard time parsing the string HTML tag and save it in a PHP array.

    I suggest that you use an HTML parser, such as simplehtmldom. This will allow you to traverse over HTML DOM just the way you want.

    Here is a quick and dirty UL walking script:

    <?php
        require_once( "simplehtmldom/simple_html_dom.php" );
        $DOM = file_get_html( "test.htm" );
        $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[ $li->find( "a", 0 )->plaintext ] = $arar;
            }
        }
        WalkUL( $DOM->find( "ul", 0 ), $ARR );
        print_r( $ARR );
    ?>
    

    Its output, not exactly as you wanted it but close:

    Array
    (
        [Folder 1] => Array
            (
                [Child 1.1] => Array
                    (
                    )
                [Folder 1.1] => Array
                    (
                        [Child 1.1.1] => Array
                            (
                            )
                    )
                [Folder 1.2] => Array
                    (
                        [Child 1.2.1] => Array
                            (
                            )
                        [Child 1.2.2] => Array
                            (
                            )
                    )
            )
        [Folder 2] => Array
            (
                [Child 2.1] => Array
                    (
                    )
                [Child 2.2] => Array
                    (
                    )
            )
        [Outer Child] => Array
            (
            )
    )
    
    0 讨论(0)
  • 2021-01-16 11:29

    Instead of messing with html you should submit the tree data in a more programmer-friendly format.

    $('#saveButton').click(function() {
        var treeData = $.tree.reference($('#sortableTree')).get(undefined, 'json');
        var tmp = serializeTree(treeData, 0);
        // now json-encode tmp and submit it
    });
    
    function serializeTree(nodes, parent)
    {
        var parents = {};
        var childOrder = []
        var childOrders = {};
        for(var i = 0; i < nodes.length; i++)
        {
            var node = nodes[i];
            var id = node.attributes.id.substr(5); // assuming the id looks like 'abcd-ID'
            parents[id] = parent;
            childOrder.push(id);
            if(node.children)
            {
                var tmp = serializeTree(node.children, id);
                for(var id in tmp[0])
                    parents[id] = tmp[0][id];
                for(var id in tmp[1])
                    childOrders[id] = tmp[1][id]
            }
        }
    
        childOrders[parent] = childOrder;
    
        return [parents, childOrders];
    }
    
    0 讨论(0)
  • 2021-01-16 11:36

    Just for referecence: there's also a jQuery plugin for serializing a HTML DOM Tree of <ol> and <ul> lists: http://www.drakedata.com/serializetree/sampleTree.html - it converts a nested structure of <ol> and <ul> lists to a string which can be used as an associated php array.

    0 讨论(0)
  • 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("#</?[a-z]+[^>]*>(.+</[a-z]+[^>]*>|)#is","",$li->innertext)) ] = $arar;
        }
    }
    WalkUL( $DOM->find( "ul", 0 ), $ARR );
    echo "<pre>"; print_r( $ARR );
    
    0 讨论(0)
提交回复
热议问题