[ create a new paste ] login | about

Link: http://codepad.org/tGL6eFlI    [ raw code | output | fork ]

tml - PHP, pasted on Jun 1:
<?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);


Output:
1
2
3
4
5
6
7
8
9
10
11
12
array(2) {
  ["Cascading Style Sheets"]=>
  string(129) "Style sheets are used to provide presentational suggestions.
Documents structured using XML or HTML are able to make use of them."
  ["Content Management"]=>
  array(2) {
    [0]=>
    string(76) "The process of collecting, managing and publishing content to various media."
    [1]=>
    string(30) "A dirty job that no one wants."
  }
}


Create a new paste based on this one


Comments: