Alternative to mb_convert_encoding with HTML-ENTITIES charset(使用 HTML-ENTITIES 字符集替代 mb_convert_encoding)
问题描述
我有以下代码:
mb_convert_encoding($string, 'HTML-ENTITIES', 'utf-8');
我需要一个完全相同但不使用任何 mb_* 函数的替代代码(mb 扩展在某些环境中不可用).
我以为
utf8_decode(htmlentities($string, ENT_COMPAT, 'utf-8'));
应该完全一样,但不幸的是它没有.
我玩了一会儿,发现这很有趣.似乎第二部分也运行htmlspecialchars".一定是 mb_convert_encoding 中的一些错误,因为 htmlentities 没有正确运行.
如果您对结果运行 htmlspecialchars_decode,您将获得与使用 mb_convert_encoding 完全相同的结果.
代码:
$string = '测试:!"$%&/()=ÖÄÜöäü<<';echo mb_convert_encoding($string, 'HTML-ENTITIES', 'utf-8')."
";echo htmlspecialchars_decode(utf8_decode(htmlentities($string, ENT_COMPAT, 'utf-8', false)));
这是上面代码的演示:http://sandbox.onlinephpfunctions.com/code/715acade3b8337d9529e4
这里有一个没有 htmlspecialchars_decode 的演示,以显示您的问题:http://sandbox.onlinephpfunctions.com/code/5c4a32bf99aa8fd6246c4a77132a023d32945363" rel="noreferrer">http://sandbox.onlinephpfunctions.com/code/5c4a32bf99aa8fd6246c4a77132A
I have the following code:
mb_convert_encoding($string, 'HTML-ENTITIES', 'utf-8');
I need to have an alternative code which does exactly the same but does not use any mb_* functions (the mb extension is not available on some environments).
I thought that
utf8_decode(htmlentities($string, ENT_COMPAT, 'utf-8'));
should do exactly the same, but unfortunately it does not.
I played around a bit, and find this very interesting. It seems like the second part also runs "htmlspecialchars". Must be some bug in mb_convert_encoding, as htmlentities is not run correctly.
If you run htmlspecialchars_decode over the result, you get exactly the same as if you would use mb_convert_encoding.
The code:
$string = 'Test:!"$%&/()=ÖÄÜöäü<<';
echo mb_convert_encoding($string, 'HTML-ENTITIES', 'utf-8')."
";
echo htmlspecialchars_decode(utf8_decode(htmlentities($string, ENT_COMPAT, 'utf-8', false)));
Here a demo of the code above: http://sandbox.onlinephpfunctions.com/code/715acade3b8337d9c9e48e58deee2a237015c259
And here a demo without htmlspecialchars_decode, to show your problem: http://sandbox.onlinephpfunctions.com/code/5c4a32bf99aa8fd6246c4a77132a023d32945363
这篇关于使用 HTML-ENTITIES 字符集替代 mb_convert_encoding的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 HTML-ENTITIES 字符集替代 mb_convert_encoding


基础教程推荐
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01