htmlentities()使汉字无法使用 [英] htmlentities() makes Chinese characters unusable

查看:84
本文介绍了htmlentities()使汉字无法使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个Web应用程序,允许用户在文本区域中输入自己的html.我们将这些数据保存到我们的数据库中.

we have a web application where we allow users to enter their own html in a text area. We save that data to our database.

当我们将html数据加载到文本区域时,我们当然会在将html数据投入文本区域之前使用htmlentities().否则,用户可以将其保存在文本区域内,并且在将其加载到文本区域时,我们的应用程序将损坏.

When we load the html data into the text area, of course, we use htmlentities() before throwing the html data into the textarea. Otherwise users could save inside the textarea and our application would break when loading that into the textarea.

这很好用,除了输入中文字符(以及其他可能的语言,例如阿拉伯语,日语)时.

this works great, except when entering Chinese characters (and probably other languages such as Arabic, Japanese).

htmlentities()使中文文本无法使用,如下所示: 当我在将输入的html加载到文本区域之前删除htmlentities()时,汉字显示得很好,但是然后我们遇到了HTML干扰我们的文本区域的问题,尤其是当用户在文本区域内输入时.

The htmlentities() makes the chinese text unusable like this: �¨�³�¼�§ï When I remove the htmlentities() before loading the entered html into the text area, Chinese characters show up just fine, but then we have the problem of HTML interfering with our textarea, especially when a users enters inside the text area.

我希望这是有道理的.

有人知道我们如何安全正确地允许在中文文本区域内使用中文,日文,...等语言,同时仍然可以安全地在文本区域内加载任何html吗?

Does anyone know how we can safely and correctly allow languages such as Chinese, Japanese, ... to be used inside our text area, while still being safe for loading any html inside our text area?

推荐答案

您是否尝试过使用 htmlspecialchars ?

我目前在生产中使用它,这很好.

I currently use that in production and it's fine.

$foo = "我的名字叫萨沙"
echo '<textarea>' . htmlspecialchars($foo) . '</textarea>';

或者,

$str = "&#20320;&#22909;";
echo mb_convert_encoding($str, ‘UTF-8′, ‘HTML-ENTITIES’);

中找到http://www.techiecorner.com/129/php-how-to-convert-iso-character-htmlentities-to-utf-8/

这篇关于htmlentities()使汉字无法使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆