header('Content-type: text/plain');
function feed_cdata($tag, $data) {
if (strpos($data, '<') === false && strpos($data, '&') === false) {
return array('text', $data);
}
$parser = xml_parser_create();
xml_parse($parser, '
' . $data . '
', true);
$code = xml_get_error_code($parser);
xml_parser_free($parser);
if (!$code) {
return array('xhtml', $data);
}
if (strpos($data, ']]>') == false) {
return array('html', "");
} else {
return array('html', htmlspecialchars($data));
}
}
function atom_cdata($tag, $data) {
list($type, $cdata) = feed_cdata($tag, $data);
if ($type == 'xhtml') {
$cdata = "$cdata
";
}
return '<' . $tag . ' type="' . $type . '">' . $cdata . '' . $tag . '>';
}
function rss_cdata($tag, $data) {
list($type, $cdata) = feed_cdata($tag, $data);
if ($type == 'xhtml') {
$cdata = "";
}
return '<' . $tag . '>' . $cdata . '' . $tag . '>';
}
echo atom_cdata('title','plain') . "\n";
echo atom_cdata('title','plain>text') . "\n";
echo atom_cdata('title','a b') . "\n";
echo atom_cdata('title','a b') . "\n";
echo atom_cdata('title','a bold move') . "\n";
echo atom_cdata('title','a bad move') . "\n";
echo atom_cdata('title','< ]]>') . "\n";
echo "\n";
echo rss_cdata('title','plain') . "\n";
echo rss_cdata('title','plain>text') . "\n";
echo rss_cdata('title','a b') . "\n";
echo rss_cdata('title','a b') . "\n";
echo rss_cdata('title','a bold move') . "\n";
echo rss_cdata('title','a bad move') . "\n";
echo rss_cdata('title','< ]]>') . "\n";
?>