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

tex*_*xai 2 php string unicode utf-8 multibyte

测试字符串:

$s = "convert this: ";
$s .= "–, —, †, ‡, •, ?, ?, ?, ?, ©, ® y ™, ?, ?, ?, ?, ?, ?, ™, ?, ?, ?, ?, ?, ? ";
$s .= "but, not convert ordinary characters to entities";
Run Code Online (Sandbox Code Playgroud)

Mar*_*c B 11

$encoded = mb_convert_encoding($s, 'HTML-ENTITIES', 'UTF-8'); 
Run Code Online (Sandbox Code Playgroud)

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