doupu1957 2016-07-25 12:22
浏览 48
已采纳

如何在数据库中插入和检索“html代码”?

Can anyone tell me how do I properly take special characters as input from a textbox, and insert it into database and retrieve it and display it on another textbox. For example, how do I deal with content like this, <html><body><anything>" " ' its has html tag, special chars and also some random text which looks like html tag, but is not. Or is there any way to deal with it in codeigniter?

My code example are.

<input type="text" id="videoTitle" name="title" value="<?php echo htmlentities($videoToEdit['title']); ?>> 

to take input from user. It is also echoing the same content in the input field.

$data['title'] = htmlentities($this->input->post('title'));

to store the user input.

<strong><?php echo html_entity_decode($video['title']); ?></strong>

to display the content in some div. And doing this, the value stored in the database is &amp;lt;body&amp;gt; &lt;Some&gt; title &lt;l&gt;&quot;m&quot; and the output I get is <body> title "m". The solution I'm looking for is similar to what stackoverflow does. Like, we can input whatever character we need and it is displayed exactly as it is in the comments below.

  • 写回答

1条回答 默认 最新

  • douqiaolong0528 2016-07-25 12:24
    关注

    You can do something like htmlentities for encoding and saving it to DB:

    htmlentities($textareaContents);      // ==> Send to DB
    

    And while retrieving, you can make use of html_entity_decode:

    echo html_entity_decode($textFromDB); // ==> Display on HTML
    

    In short, the above functions do these:

    htmlentities("<html>");             // ==> "&lt;html&gt;"
    html_entity_decode("&lt;html&gt;"); // ==> "<html>"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决