【问题标题】:What is the best method for storing (encoding/decoding) HTML in a MSSQL database using PHP?使用 PHP 在 MSSQL 数据库中存储(编码/解码)HTML 的最佳方法是什么?
【发布时间】:2015-03-12 16:00:12
【问题描述】:

目前我正在使用设置为 POST 的表单中的 textarea:

<textarea name="post_content">HTML CODE PASTED HERE AND THEN SUBMITTED</textarea>

在提交时,PHP 启动:

<?php 

    //Encode the HTML for storage (is this the best method?)
    $page_content = filter_input(INPUT_POST, 'page_content', FILTER_SANITIZE_SPECIAL_CHARS);

    //SQL to insert into a table
    $sql = "INSERT INTO advanced_cms_pages (page_content) VALUES(?)";
    $params = array(
        array(&$page_content, NULL, NULL, SQLSRV_SQLTYPE_TEXT)
        );        
    $stmt = sqlsrv_prepare( $connection, $sql, $params );

    //Check if the sql was successful
    if (!sqlsrv_execute($stmt)) {  
        die (slqsrv_errors());
    }

?>

然后输出到浏览器:

<?php

    //Decode on output
    echo htmlspecialchars_decode($page['page_content']);

?>

这似乎不是一个完整的解决方案,因为有时我仍然会得到未正确编码/解码的字符。有什么建议么?有没有经过验证的在 mssql 数据库中存储 HTML 的方法?

示例:

  • 换行是一个问题 "\n" 出现,因为它的 html 代码等效 &amp;#10;
  • 回车也一样,“\r”输出为&amp;#13;
  • 我还没有看到的其他可能。

服务器构建:

  • PHP 5.4.14
  • MSSQL 2008 R2
  • IIS7.5

【问题讨论】:

  • 您能否发布正确编码/解码的字符示例?
  • 添加了示例,我现在只能找到两个,换行符和回车符。不过,我担心如果这些没有得到适当的转换,还会有其他的。

标签: php html sql-server


【解决方案1】:

您可以使用 htmlentities 函数进行编码,使用 html_entity_decode 进行解码。

这对我来说是更好的方法。我使用 UTF8 字符集。

【讨论】:

    猜你喜欢
    • 2017-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-08
    • 2010-12-25
    • 2020-08-10
    相关资源
    最近更新 更多