Created
April 9, 2010 18:15
-
-
Save tml/361425 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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