duankeng2026 2015-09-06 03:12
浏览 204
已采纳

使用textarea中的htmlentities转义HTML,但转回HTML用于数据库

So I have slight problem. The PHP program I am working on allows web designers to post some code sometimes, and is put into a backend database. Sometimes the designers may also want to discuss html entities.

So let's say the designer adds a line of code to database like this:

<p>hellos friend</p>

So I use PDO to stick this line in the database without escaping it, and everything is fine, it shows up good. Now, I want the web designer to be able to edit this, so when I pull it out of database I use this code after:

$post = htmlentities($post);

It is good now that I can insert this into my editor:

<textarea>$post</textarea>

But we have problem because when this guy edit this code, he submit and it go back into the database and now it got HTML entities and has & lt ; instead of < and & gt ; instead of > (it is hard to type this on SA it re-encodes it)

&lt;p&gt;hellos friend&lt;/p&gt;

So now it's wrong in database, so when I display it again, it show the entities.

So maybe I can run the opposite of htmlentities after editing it and re-add all the entities, if there is such a thing, but that brings another problem:

What if a web designer is telling other guy "Hey man, this is what an html entity is, it is typed like this: & lt; you should use it"

Then THAT is going to turn out to get stuck back into NOT being an entity, you see what I mean? Is there a solute?

  • 写回答

1条回答 默认 最新

  • duanla8800 2015-09-06 05:34
    关注

    I think you are looking for the html_entity_decode() function. It is the reverse of htmlentities(). It converts the &lt back into <

    http://php.net/manual/en/function.html-entity-decode.php

    For your second issue you would need to have the designers escape the html entities somehow. I don't think PHP has a way to escape them but I could be wrong. It might be something you would have to implement yourself, like have the designer put a \ in front of the entity to set it apart from the entities that are suppose to be converted to HTML and then parse the input looking for escaped entities.

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

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用