Created
March 25, 2010 06:09
-
-
Save dahlia/343245 to your computer and use it in GitHub Desktop.
xmlbuild.php
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 | |
class XmlBuilder { | |
function __call($tag, array $contents) { | |
$content = ''; | |
foreach ($contents as $c) { | |
if ($c instanceof XmlBuilder_Element) { | |
$content .= $c; | |
} else if (is_array($c)) { | |
$attrs = array_merge($array, $c); | |
} else { | |
$content .= htmlsepcialchars($c); | |
} | |
} | |
foreach ($attrs as $name => $value) { | |
$attrstr[] = $name . '="' . htmlspecialchars($value) . '"' | |
} | |
return "<$tag " . join(' ', $attrstr) . ($content == '' ? " />" : ">$content</$tag>"); | |
} | |
} | |
class XmlBuilder_Element { | |
public $content; | |
function __construct($content) { | |
$this->content = $content; | |
} | |
function __toString() { | |
return $this->content; | |
} | |
} | |
##################################### | |
$x = new XmlBuilder; | |
$x->element(array('attr' => 'value'), | |
$x->child('nodes'), | |
$x->child2()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment