tags:

views:

51

answers:

2
+1  Q: 

HTML list to array

How can I parse multilevel HTML list and get an array by php

+1  A: 

With an HTML parser.

Ignacio Vazquez-Abrams
Thanks a lot , but this parses HTML list like $OL[0],$OL[1],$OL[2] ..., I need multilevel parser like this $OL[0][0],$OL[0][1] and $OL[1][0], $OL[1][1], $OL[1][2] etc...
Artyom Chakhoyan
So you'll have to apply a bit of elbow grease then.
Ignacio Vazquez-Abrams
A: 

I am trying this code

$text='<ol>
            <li>31</li>
            <li>32</li>
            <li>33</li>
            <li>34</li>
            <li>
                <ol>
                    <li>341</li>
                    <li>342</li>
                    <li>343</li>
                    <li>344</li>
                    <li>
                        <ol>
                            <li>3441</li>
                            <li>3442</li>
                            <li>3443</li>
                            <li>3444</li>
                        </ol>
                    </li>
                </ol>
            </li>
        </ol>';


$html = str_get_html($text);
foreach( $html->find('ol') as $ol)
{
    $array[] = $ol->innertext; 
}

print_r($array);

Here is the result

Array
(
    [0] => 
            <li>31</li>
            <li>32</li>
            <li>33</li>
            <li>34</li>
            <li>
                <ol>
                    <li>341</li>

                    <li>342</li>
                    <li>343</li>
                    <li>344</li>
                    <li>
                        <ol>
                            <li>3441</li>
                            <li>3442</li>

                            <li>3443</li>
                            <li>3444</li>
                        </ol>
                    </li>
                </ol>
            </li>

    [1] => 
                    <li>341</li>

                    <li>342</li>
                    <li>343</li>
                    <li>344</li>
                    <li>
                        <ol>
                            <li>3441</li>
                            <li>3442</li>

                            <li>3443</li>
                            <li>3444</li>
                        </ol>
                    </li>

    [2] => 
                            <li>3441</li>
                            <li>3442</li>

                            <li>3443</li>
                            <li>3444</li>

)

But I need something Like this

Array
(
    [0] => Array
               (
              [0] => Array
                         (
                          [0] =>.... 
Artyom Chakhoyan