Simple html dom - how get dt/dd elements to array? [duplicate]



<strong>Possible Duplicate:</strong><br /><a href="https://stackoverflow.com/questions/4057574/get-data-only-from-html-table-used-preg-match-all-in-php" rel="nofollow">Get data only from html table used preg_match_all in php</a>



<div class="table"> <dl> <dt>ID:</dt> <dd>632991</dd> <dt>Type:</dt> <dd>NEW</dd> <dt>Body Type:</dt> <dd>Compact</dd> </dl> </div>

What's is the best way to get this using simple_html_dom in PHP:


$option = array( 'id' => 632991, 'Type' => 'NEW', 'Body Type' => 'Compact' );


You could use XPath:

<a href="https://stackoverflow.com/questions/6366351/getting-dom-elements-by-class-name" rel="nofollow">Getting DOM elements by classname</a>

<a href="http://php.net/manual/en/domdocument.getelementsbytagname.php" rel="nofollow">Get Element ByTag Name</a>

<a href="https://stackoverflow.com/questions/8144061/using-php-to-get-dom-element" rel="nofollow">Using PHP to get DOM Element</a>

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


<?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 ($nodes as $node) { var_dump(getArray($node)); } function getArray($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


  • php , simple_html_dom.php, get selected option
  • Adjust PHP code Retrieve the DOM from a given URL
  • Getting Tag Names with BeautifulSoup
  • does PHP simple html dom parser conflict with wordpress?
  • getting an error reading simpleDomObject
  • Is it possible to do 'not' matching in XPath?
  • Reading DOMDocument and find elements using CSS selectors
  • Use Greasemonkey to remove table
  • HTML elements in lxml get incorrectly encoded like Най
  • Selenium: find element by visible Text
  • Whitelist a set of properties from a multidimensional json array and delete the rest
  • Why PHP DOMDocument loadHTML doesn't work for Persian characters?
  • web scrape with rvest
  • How do I retrieve the text in a table column using Selenium RC?
  • Failing to get duration of youtube video using xpath
  • Adaptive Triggers in Resource Dictionary
  • Searching an XML file using PHP [closed]
  • parsing xml and html page with lxml and requests package in python
  • Available space left on drive - WinAPI - Windows CE
  • How do I access an unhandled exception in an MVC Error view?
  • Alternative To body {overflow:scroll;} That Will Prevent Page Jostling/Wriggling?
  • Play WS (2.2.1): post/put large request
  • Google Custom Search with transparent background
  • FB SDK and cURL: Unknown SSL protocol error in connection to graph.facebook.com:443
  • Optimizing database types to compact database (SQLite)
  • vba code to select only visible cells in specific column except heading
  • Upload files with Ajax and Jquery
  • Do I've to free mysql result after storing it?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • A cron job substitute?
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Benchmarking RAM performance - UWP and C#
  • Angular 2 constructor injection vs direct access
  • How to stop GridView from loading again when I press back button?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • How to Embed XSL into XML
  • To Get the radio button value in ruby on rails