如何使用PHP将一些多字节字符转换为数字html实体?

如何使用PHP将一些多字节字符转换为数字html实体?,php,string,unicode,utf-8,multibyte,Php,String,Unicode,Utf 8,Multibyte,测试字符串: $s = "convert this: "; $s .= "–, —, †, ‡, •, ≤, ≥, μ, ₪, ©, ® y ™, ⅓, ⅔, ⅛, ⅜, ⅝, ⅞, ™, Ω, ℮, ∑, ⌂, ♀, ♂ "; $s .= "but, not convert ordinary characters to entities"; 嗯,它不能正常工作。幸运的是,php网站上有人似乎正确地翻译了多字节字符 $encoded = mb_convert_encoding($s, 'HTM

测试字符串:

$s = "convert this: ";
$s .= "–, —, †, ‡, •, ≤, ≥, μ, ₪, ©, ® y ™, ⅓, ⅔, ⅛, ⅜, ⅝, ⅞, ™, Ω, ℮, ∑, ⌂, ♀, ♂ ";
$s .= "but, not convert ordinary characters to entities";
嗯,它不能正常工作。幸运的是,php网站上有人似乎正确地翻译了多字节字符

$encoded = mb_convert_encoding($s, 'HTML-ENTITIES', 'UTF-8'); 

假设您的输入字符串是UTF-8,这应该将大部分内容编码为数字实体。

我曾将ascii解码为html编码文本(&#xxxx)

但是为什么呢?如果文档编码正确,则不需要这样做。@Pekka:我的问题不是渲染数据,而是存储数据。我无法更改数据库结构或配置字段。如果您的数据库无法存储非ASCII字符,则需要修复数据库,而不是将数据混在某种特定的编码格式中。保持数据库字符串的原始形式。谢谢,这正是我需要的。@Marc B,你太棒了,这就是我要找的。非常有用