header('Content-type: text/plain');
function textConstruct($element, $base, $text) {
$type = null;
$parser = xml_parser_create();
if (strlen($text)>64 and xml_parse($parser, "
$text
")) {
if (strpos($text, '<') !== false) {
$type = 'xhtml';
$text = "$text
";
}
} else {
$type = 'html';
if (strpos($text, ']]>') === false) {
$text = "";
} else {
$text = htmlspecialchars($text);
}
}
xml_parser_free($parser);
$result = "<$element";
if ($type) $result .= " type='$type'";
if ($base) $result .= " xml:base='$base'";
$result .= ">$text$element>";
return $result;
}
?>
print "\n" . textConstruct('title', 'http://example.com/', 'plain');
print "\n" . textConstruct('title', null, 'It’s just data');
print "\n" . textConstruct('title', null,
'short text with some emphasized and bold words.');
print "\n" . textConstruct('summary', null,
'lengthy text with some emphasized and bold words.');
print "\n" . textConstruct('summary', null, 'x
y');
print "\n" . textConstruct('content', null, 'x
y ]]> z');
?>