PHP-将非ASCII字符转换为不带mbstring的十六进制实体


PHP - Convert Non-ASCII Characters to hex Entities Without mbstring

我想将任何Unicode字符串转换为十六进制HTML实体,ASCII字符除外。所以像这样的字符串

Text goes here. Here's だ and here's ã.

转换为

Text goes here. Here's &#12384 and here's &#227.

作为参考,这个问题有一个函数可以将所有字符转换为数字实体,但它需要mbstring,我不能使用(我也不能使用PHP 5.3.10之后的任何功能)。如何使用PHP 将所有字符转化为等效的html实体

这不是我的代码。

我用";php将unicode转换为html";并发现:

https://af-design.com/2010/08/17/escaping-unicode-characters-to-html-entities-in-php/

其中包括:

function unicode_escape_sequences($str)
{
 $working = json_encode($str);
 $working = preg_replace('/'''u([0-9a-z]{4})/', '&#x$1;', $working);
 return json_decode($working);
}

这个网页上还有很多其他的例子,但这个看起来就像你想要的。