简体   繁体   中英

php need help to create nested array using parsing html element

I need a help to create a nested array from element of html file using dom method this is the code

<?php
$html = '
    <p>text1</p>
    <ul>
        <li>list-a1</li>
        <li>list-a2</li>
        <li>list-a3</li>
    </ul>
    <p>text2</p>
    <ul>
        <li>list-b1</li>
        <li>list-b2</li>
        <li>list-b3</li>
    </ul>
    <p>text3</p>';

$doc = new DOMDocument();
$doc->loadHTML($html);
foreach ($doc->getElementsByTagName('p') as $link) {
    echo $link->nodeValue."\n", PHP_EOL;
}
foreach ($doc->getElementsByTagName('ul') as $link) {
    $books = $link->getElementsByTagName('li');
    foreach ($books as $book) {
        echo $book->nodeValue, PHP_EOL;
        // $links3[] = array( $ii=> $book->nodeValue, );
        //$ii++;
    }
}
?> 

and this is the Program Output:

text1
text2
text3
list-a1
list-a2
list-a3
list-b1
list-b2
list-b3

but i need to get this output in the same order of original html

text1
list-a1
list-a2
list-a3
text2
list-b1
list-b2
list-b3
text3

without using preg or replace method !!!

To print the values

<?php

    $html = '
        <p>text1</p>
        <ul>
            <li>list-a1</li>
            <li>list-a2</li>
            <li>list-a3</li>
        </ul>
        <p>text2</p>
        <ul>
            <li>list-b1</li>
            <li>list-b2</li>
            <li>list-b3</li>
        </ul>
        <p>text3</p>
    ';

    $doc = new DOMDocument();
    $doc->loadHTML($html);

    foreach ($doc->getElementsByTagName('body')->item(0)->childNodes as $node) {
        if ($node->nodeType === XML_ELEMENT_NODE) {
            if($node->nodeName == 'p'){
                    echo $node->nodeValue."\n", PHP_EOL;

            }elseif($node->nodeName == 'ul'){
                $books = $node->getElementsByTagName('li');
                foreach ($books as $book) {
                    echo $book->nodeValue, PHP_EOL;
                }

            }
        }
    }

    ?>

Output

text1 
list-a1 
list-a2 
list-a3 
text2 
list-b1 
list-b2 
list-b3 
text3

To print in the form of nested array

<?php

$html = '
    <p>text1</p>
    <ul>
        <li>list-a1</li>
        <li>list-a2</li>
        <li>list-a3</li>
    </ul>
    <p>text2</p>
    <ul>
        <li>list-b1</li>
        <li>list-b2</li>
        <li>list-b3</li>
    </ul>
    <p>text3</p>
';

$result = array();

$doc = new DOMDocument();
$doc->loadHTML($html);
$i = 0;
foreach ($doc->getElementsByTagName('body')->item(0)->childNodes as $node) {
    if ($node->nodeType === XML_ELEMENT_NODE) {
        if($node->nodeName == 'p'){
            $result[$node->nodeName][$i] = $node->nodeValue;

        }elseif($node->nodeName == 'ul'){
            $result[$node->nodeName][$i] = array();
            $books = $node->getElementsByTagName('li');
            foreach ($books as $book) {
                $result[$node->nodeName][$i][$book->nodeName][] = $book->nodeValue;
            }

        }
        $i++;
    }
}
var_dump($result);

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM