douzhenao6515 2018-02-09 04:20
浏览 66
已采纳

在php中将特殊字符从sql转换为普通字符

1) I have a textarea in my html. Inside the textarea I wrote: <i>ABC Enterprise</i>. When saving into the sql database it saved as &lt;i&gt;XYZ Enterprise&lt;/i&gt;

2) Does anyone know how to retain < and </> when saving into the database without converting? If this is not possible, does anyone know how to convert &lt;i&gt;XYZ Enterprise&lt;/i&gt; to <i>ABC Enterprise</i> in php? I need the string to maintain this form <i>ABC Enterprise</i> in php not html.

I have tried preg_replace("/&([a-z])[a-z]+;/i", "$1", htmlentities($company)), iconv('utf-8', 'ascii//TRANSLIT', $company), htmlspecialchars($compnay), many other ways I happened to stumble upon on stackoverflow but nothing seemed to work. Any help?

  • 写回答

1条回答 默认 最新

  • dongqiang4986 2018-02-09 04:44
    关注

    To specifically answer your question:

    1. How to retain <> and </> when inserting into the DB? [paraphrased, emphasis added]

      Simple: don't modify your data. As discussed below, however, be smart about it and insert the data using a prepared statement.

    Why is your data being changed? Most likely because your code is doing some form of modification of the data before putting it in the database. In PHP, this generally means one of:

    The general advice for years was simply "escape all your data or suffer the XSS/CSRF/Sql Injection/other attack consequences!" The problem is that there are nuances of when and how to escape and in the zeal for security, many websites over do it. As you've described your situation, I would consider:

    1. When inserting into the DB: use prepared statements, rather than manual escaping.
    2. When pulling from the DB: be judicious when you apply escaping techniques.

    A prepared statement is where you tell the database the format of what you're going to send, then send the data in a separate communication. If there's anything awry, the DB knows best how to find it. For example:

    $pstmt = $dbh->prepare('INSERT INTO tab (html) VALUES (?)');
    $pstmt->execute(array($_POST['my_textarea']));
    

    Note the lack of any sanitization, using the $_POST variable directly. What the user sent to you is what you put in the DB, with zero modification. Because the DB server was sent a format first, it will not allow any ulterior SQL injection shenanigans.

    However, when pulling data out of the DB, you need to be careful of exactly what data goes where. For example, to allow < and > characters inside of the content might be foolhardy, depending on your context. I'll leave it to you to decide whether you want to escape the output inside of your <textarea>:

    echo "<textarea>$textarea_content_as_retrieved_from_db</textarea>";
    

    or

    echo '<textarea>' . htmlentities( $textarea_content_as_retrieved_from_db ) . '</textarea>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来