donglu8344812 2012-05-08 01:37
浏览 64
已采纳

htmlspecialchars()正在翻译其他特殊字符

htmlspecialchars() appears to be translating special chars like the following: āķūņūķī into their respective entity number:

ā ķ ū ņ ū ķ ī

While some remain untranslated such as:

žš

I would like htmlspecialchars() (or some other function) to not translate these alphabetical type of characters... So that it only translates the following (as it seems to indicate on the php.net manual):

  1. '&' (ampersand) becomes '&'
  2. '"' (double quote) becomes '"' when ENT_NOQUOTES is not set.
  3. "'" (single quote) becomes ''' only when ENT_QUOTES is set.
  4. '<' (less than) becomes '&lt;'
  5. '>' (greater than) becomes '&gt;'

The reason why I need this is because after a POST request, i am running this user input through htmlspecialchars() before placing it back into a new set of html inputs. Characters such as &,",',<,>, need to be translated so to not cause display errors etc. But i need the special chars such as 'āķūņūķī' remain unchanged. Else the user will be very confused.

  • 写回答

2条回答 默认 最新

  • dongshuo8756 2012-05-08 01:48
    关注

    Set the third parameter as UTF-8:

    echo htmlentities('āķūņūķī', ENT_QUOTES, 'UTF-8');
    

    The default encoding for htmlspecialchars is ISO-8859-1.

    Test case:

    var_dump(htmlentities('āķūņūķī'));
    var_dump(htmlentities('āķūņūķī', ENT_QUOTES, 'UTF-8'));
    

    Output:

    string(84) "&Auml;�&Auml;&middot;&Aring;&laquo;&Aring;�&Aring;&laquo;&Auml;&middot;&Auml;&laquo;"
    string(14) "āķūņūķī"
    

    http://codepad.org/MCaDosQ5

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法