douhan9619 2013-10-25 14:31
浏览 17
已采纳

htmlspecialchars()x htmlentities()[复制]

This question already has an answer here:

I have read their documentation, but I still don't get when to use each of them and their difference.

Let's consider the situation of having a general string in a variable and needing to echo it inside HTML code. If it has any HTML markup in it, I want it converted to HTML code (< replaced by &lt;, & replaced by &amp;. If it has UTF special chars that aren't available in HTML code, it's replaced by HTML number ( replaced by &#8226;).

What's the best function for that?

A harder need: unprintable chars, like , char(10), char(13), etc, be replaced by their number code, in the case the string is printed inside <pre> or any special textarea so that the string be dumped.

</div>
  • 写回答

1条回答 默认 最新

  • doulin2947 2013-10-25 15:29
    关注

    htmlentities is a workaround for not having set the character type of the document properly. htmlspecialchars is the correct function to use for merely writing text into an HTML document.

    As to your second question, I think you're looking for addcslashes.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥20 sim800c模块 at指令及平台
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计