public function _xml_entities($s, $hex = true)
{
// if the string is empty
if (empty($s)) {
// just return it
return $s;
}
$s = (string) $s;
// create the return string
$r = '';
// get the length
$l = strlen($s);
// iterate the string
for ($i = 0; $i < $l; $i++) {
// get the value of the character
$o = $this->_unicode_ord($s, $i);
// valid characters
$v = $o >= 9 && $o <= 13 || $o == 32 || $o == 33 || $o >= 35 && $o <= 37 || $o >= 40 && $o <= 47 || $o >= 48 && $o <= 57 || $o == 58 || $o == 59 || $o == 61 || $o == 63 || $o == 64 || $o >= 65 && $o <= 90 || $o >= 91 && $o <= 96 || $o >= 97 && $o <= 122 || $o >= 123 && $o <= 126;
// if it's valid, just keep it
if ($v) {
$r .= $s[$i];
// &
} elseif ($o == 38) {
$r .= '&';
// <
} elseif ($o == 60) {
$r .= '<';
// >
} elseif ($o == 62) {
$r .= '>';
// '
} elseif ($o == 39) {
$r .= ''';
// "
} elseif ($o == 34) {
$r .= '"';
// unknown, add it as a reference
} elseif ($o > 0) {
if ($hex) {
$r .= '&#x' . strtoupper(dechex($o)) . ';';
} else {
$r .= '&#' . $o . ';';
}
}
}
return $r;
}