<?php
function ordUTF8 ($c, $index = 0, &$bytes = null)
{
$bytes = 0;
if ($index >= $len)
return false;
if ($h <= 0x7F)
{
$bytes = 1;
return $h;
}
else if ($h < 0xC2)
return false;
else if ($h <= 0xDF && $index < $len - 1)
{
$bytes = 2;
return ($h & 0x1F
) << 6
| (ord($c{$index + 1
}) & 0x3F
);
}
else if ($h <= 0xEF && $index < $len - 2)
{
$bytes = 3;
return ($h & 0x0F
) << 12
| (ord($c{$index + 1
}) & 0x3F
) << 6
| (ord($c{$index + 2
}) & 0x3F
);
}
else if ($h <= 0xF4 && $index < $len - 3) {
$bytes = 4;
return ($h & 0x0F
) << 18
| (ord($c{$index + 1
}) & 0x3F
) << 12
| (ord($c{$index + 2
}) & 0x3F
) << 6
| (ord($c{$index + 3
}) & 0x3F
);
}
else
return false;
}
function xml_entities ($utf8_string, $style='#')
{
$return = '';
{
$char = mb_substr($utf8_string, 0, 1, 'UTF-8');
$ord = ordUTF8($char, 0, $bytes);
$utf8_string = substr($utf8_string, $bytes);
if ($ord == 34)
$return .= '"';
elseif ($ord == 38)
$return .= '&';
elseif ($ord == 60)
$return .= '<';
elseif ($ord == 62)
$return .= '>';
elseif ($ord == 9 || $ord == 10 || $ord == 13)
$return .= $char;
elseif ($style == '#' && ($ord < 32 || $ord > 127 || $ord == 39))
$return .= '&#'.$ord.';';
elseif ($style == '#x' && ($ord < 32 || $ord > 127 || $ord == 39))
$return .= '&#'.dechex($ord).';';
else
$return .= $char;
}
return $return;
}
# Testing...
print xml_entities("Pound is £ & euro is € & plus or minus is ±\n");
?>