Skip to content Skip to sidebar Skip to footer

Simple Html Dom - How Get Dt/dd Elements To Array?

Possible Duplicate: Get data only from html table used preg_match_all in php HTML:
ID:

Solution 1:

You could use XPath:

Getting DOM elements by classname

Get Element ByTag Name

Using PHP to get DOM Element

Here are a lot of posts on Stackoverflow. Use the search here.

Edit:

<?php$dom = new DOMDocument();
$dom->loadHTML('<div class="table">
       <dl class="list">
            <dt>ID:</dt>
            <dd>632991</dd>
            <dt>Type:</dt>
            <dd>NEW</dd>
            <dt>Body Type:</dt>
            <dd>Compact</dd>
        </dl>
    </div>');

$nodes = $dom->getElementsByTagName('dl');
foreach ($nodesas$node) {
    var_dump(getArray($node));
}

functiongetArray($node) { 
    $array = false; 

    if ($node->hasAttributes()) { 
        foreach ($node->attributes as$attr) { 
            $array[$attr->nodeName] = $attr->nodeValue; 
        } 
    } 

    if ($node->hasChildNodes()) { 
        if ($node->childNodes->length == 1) { 
            $array[$node->firstChild->nodeName] = $node->firstChild->nodeValue; 
        } else { 
            foreach ($node->childNodes as$childNode) { 
                if ($childNode->nodeType != XML_TEXT_NODE) { 
                    $array[$childNode->nodeName][] = getArray($childNode); 
                } 
            } 
        } 
    } 
    return$array; 
} 
?>

The function getArray is from php.net

Post a Comment for "Simple Html Dom - How Get Dt/dd Elements To Array?"