= 0x00 ) && ( $chval <= 0x7F )) { $bytes = 1; $outputval = $chval; // Since 7-bit ASCII is unaffected, the output equals the input } else { for ($i = 5; $i > 0; $i--) { if (($chval >> $i) == ( (pow(2, (8 - $i))) - 2)) { $bytes = 7 - $i; $outputval = $chval & ((2 ^ $i) - 1); } } } if ($bytes !== 0) { if ($pos + $bytes - 1 < strlen($utf8_text)) { while ($bytes > 1) { $pos++; $bytes--; $outputval = $outputval * 0x40 + ( (ord($utf8_text[$pos])) & 0x3F ); } if ($outputval != 0) { $output[] = $outputval; } } } } $htmloutput = ""; foreach ($output as $unistr) { if ($bytes < 3) { $htmloutput .= "&#" . str_pad($unistr, 3, "0", STR_PAD_LEFT) . ';'; } else { $htmloutput .= "&#" . $unistr . ';'; } } return $htmloutput; }