Hwe*_*nde 1 php unicode utf-8 html-entities
我有以下字符串:
$string = "? This is some text ?";
Run Code Online (Sandbox Code Playgroud)
我想将其转换为html实体:
$string = "★ This is some text ★";
Run Code Online (Sandbox Code Playgroud)
每个人都在写的解决方案:
htmlentities("? This is some text ?", "UTF-8");
Run Code Online (Sandbox Code Playgroud)
但是htmlentities无法将所有unicode转换为html实体.所以它只是给我与输入相同的输出:
? This is some text ?
Run Code Online (Sandbox Code Playgroud)
我也尝试将这个解决方案与两者结合起来:
header('Content-Type: text/plain; charset=utf-8');
Run Code Online (Sandbox Code Playgroud)
和:
mb_convert_encoding();
Run Code Online (Sandbox Code Playgroud)
但这打印和空结果,根本不转换或错误地将星星转换为:
Â
Run Code Online (Sandbox Code Playgroud)
如何将★和所有其他unicode字符转换为正确的html实体?
htmlentities在这种情况下不起作用,但您可以尝试UCS-4对字符串进行编码,例如:
$string = "? This is some text ?";
$entity = preg_replace_callback('/[\x{80}-\x{10FFFF}]/u', function ($m) {
$char = current($m);
$utf = iconv('UTF-8', 'UCS-4', $char);
return sprintf("&#x%s;", ltrim(strtoupper(bin2hex($utf)), "0"));
}, $string);
echo $entity;
Run Code Online (Sandbox Code Playgroud)
★ This is some text ★
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1164 次 |
| 最近记录: |