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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。