- var_dump: documentation ( source)
- chr: documentation ( source)
- html_entity_decode: documentation ( source)
- dechex: documentation ( source)
<?php
function html_asciify(string $utf8String): string
{
$iterator = IntlBreakIterator::createCodePointInstance();
$iterator->setText($utf8String);
$result = '';
foreach ($iterator as $pos) {
if ($pos === 0) {
continue;
}
$codePoint = $iterator->getLastCodePoint();
$result .= $codePoint <= 127
? chr($codePoint)
: '&#x' . dechex($codePoint) . ';';
}
return $result;
}
$result = html_asciify("aaaååddd仕事bbbb\x80aaaa");
var_dump($result, html_entity_decode($result));