Wrapping CDATA
December 26th, 2008
No comments
function clean_up_xml($xml_string='', $array_of_tags)
{
if (count($array_of_tags) == 0)
{
throw new Exception('Second parameter, must be an array of strings');
}
foreach ($array_of_tags as $tag)
{
// Clean up tags, and trim whitespaces around the tag.
$needle[] = "@[\n\r\t ]*(\<[/]?{$tag}\>)[\n\r\t ]*@";
$replace[] = '$1';
// find tags that don't have CDATA, and wrap them with CDATA
$needle[] = "@\<{$tag}\>(?!\<\!\[CDATA\[)(.*?)\</{$tag}\>@ms";
$replace[] = "<{$tag}><![CDATA[$1]]></{$tag}>";
$xml_string = preg_replace($needle, $replace, $xml_string);
}
return $xml_string;
}
Explanation:
- Remove all spaces, tabs, \r, and \n characters around the desired tag, replace with just the tag itself
- Find all occurances of $tag, but not followed by <![CDATA[, and wrap contents in <![CDATA[ ... ]]>.
Categories: PHP