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

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 之后的任何功能)。 How to convert all characters to their html entity equivalent using PHP

这不是我的代码。

我使用“php 将 unicode 转换为 html”进行了简单的 Google 检查并发现:

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;', $working);
 return json_decode($working);
}

该网页上还有很多其他示例,但这个看起来正是您要找的。