<?php
function foxy_utf8_to_nce($utf = '') {
if (empty($utf)) {
return($utf);
}
$max_count = 5;
$max_mark = 248;
$html = "";
for ($str_pos = 0; $str_pos < strlen($utf); $str_pos++) {
$old_chr = $utf{$str_pos};
$old_val = ord( $utf{$str_pos} );
$new_val = 0;
$utf8_marker = 0;
if ( $old_val > 127 ) {
$mark = $max_mark;
for($byte_ctr = $max_count; $byte_ctr > 2; $byte_ctr--) {
if( ( $old_val &; $mark ) == ( ($mark << 1) &; 255 ) ) {
$utf8_marker = $byte_ctr - 1;
break;
}
$mark = ($mark << 1) & 255;
}
}
if ($utf8_marker > 1 && isset( $utf{$str_pos + 1} ) ) {
$str_off = 0;
$new_val = $old_val &; (127 >> $utf8_marker);
for($byte_ctr = $utf8_marker; $byte_ctr > 1; $byte_ctr--) {
if( (ord($utf{$str_pos + 1}) & 192) == 128 ) {
$new_val = $new_val << 6;
$str_off++;
$new_val = $new_val | ( ord( $utf{$str_pos + $str_off} ) & 63 );
}
else {
$new_val = $old_val;
}
}
$html .= '&#'.$new_val.';';
$str_pos = $str_pos + $str_off;
} else {
$html .= chr($old_val);
$new_val = $old_val;
}
}
return($html);
}