Skip to content

Instantly share code, notes, and snippets.

@tml
Created April 9, 2010 18:15
Show Gist options
  • Save tml/361425 to your computer and use it in GitHub Desktop.
Save tml/361425 to your computer and use it in GitHub Desktop.
<?php
$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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment