Skip to content

Instantly share code, notes, and snippets.

@nimmneun
Last active November 28, 2015 15:54
Show Gist options
  • Select an option

  • Save nimmneun/b4f60d8760e9fcb35697 to your computer and use it in GitHub Desktop.

Select an option

Save nimmneun/b4f60d8760e9fcb35697 to your computer and use it in GitHub Desktop.
xmlify array (with CDATA) imho a nifty way to convert an array to an XML string =)
<?php
// Convert n-dimensinal array to xml style string & wrap CDATA.
function xmlify($arr)
{
$str = null;
foreach ($arr as $k => $v) {
if (is_array($v)) {
$str .= "<$k>" . xmlify($v) . "</$k>";
} else {
if (preg_match('#[<>&\?\']#', $v, $m)) {
$v = '<![CDATA[' . $v . ']]>';
}
$str .= "<$k>" . $v . "</$k>";
}
}
return $str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment