codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
<?php # parse dl/dt/dd into an array $xml = <<<XML <dl> <dt>Cascading Style Sheets</dt> <dd><p>Style sheets are used to provide presentational suggestions.</p> <p>Documents structured using XML or HTML are able to make use of them.</p></dd> <dt>Content Management</dt> <dd>The process of collecting, managing and publishing content to various media.</dd> <dd>A dirty job that no one wants.</dd> </dl> XML; $defs = array(); $a = new DOMDocument(); $a->loadXML($xml); $xpath = new DOMXPath($a); $dl = $xpath->query('/dl')->item(0); foreach($dl->childNodes as $child) { $curr = $child->textContent; if ($child->tagName == 'dt') { $term = $curr; } if ($child->tagName == 'dd') { # it's a definition for the last 'dt' we saw if (isset($defs[$term])) { if (is_array($defs[$term])) { $defs[$term][] = $curr; } else { $prev = $defs[$term]; $defs[$term] = array($prev, $curr); } } else { $defs[$term] = $curr; } } } var_dump($defs);
Private
[
?
]
Run code
Submit