简体   繁体   English

在 php 中从 mssql 编码和解码 html 代码

[英]encode & decode html code from mssql in php

I just added a WYSIWYG editor on my website so my users can format their content any way they want.我刚刚在我的网站上添加了一个 WYSIWYG 编辑器,这样我的用户就可以以他们想要的任何方式格式化他们的内容。 The editor works fine and is successfully saving the html coded content on the database.编辑器工作正常,并成功地将 html 编码的内容保存在数据库中。

The problem is when I try to view the content from MSSQL database, here's what it shows on the webpage:问题是当我尝试从 MSSQL 数据库查看内容时,它在网页上显示如下:

�fa)2016-03-16 12:35:19.000Pg;e)@��+�!!P�P+�fa ��`��,�!H+�H+� �����,�1 � �||�`���X���sql�1 m!���1rsode!!htmlspecialchars_decode!flyerflyer9
���9����X� ������x�����`��1�r~����X�����id!1+!connection.phpflyer_id,� x����,�hpr_id,� x����,�) ����a)���]�� �|����*?���@B�r~�������������Ya�v�&������C:\inetpub\wwwroot\123\test_en_de.phpY43ectsql�9SELECT * FROM flyer_master Where flyer_id = '9����#����)`!�@#��,��,��,��)' ��Errorresultodbc_exec� h����,�

The php code to display the content is:显示内容的php代码为:

<?php

            include("connection.php");

            $flyer_id = 43 ;


            $sql = ("SELECT * FROM flyer_master Where flyer_id = '".$flyer_id."' ") or die ("Error");

            $result=odbc_exec($connect,$sql)or die(exit("Error en odbc_exec"));
            while(odbc_fetch_row($result))
            { 
                    $id = odbc_result($result,1);
                    $name = odbc_result($result,2);
                    $flyer = odbc_result($result,3);
            }


            echo $flyer;

             //$a = htmlentities($flyer);
             echo "<br>";
             echo htmlspecialchars_decode(flyer);
            //echo $de_code = html_entity_decode($flyer);
            //echo  $de_code = htmlspecialchars($flyer ,ENT_QUOTES, 'UTF-8'); 

        ?>              

Can you please help me correct this code so the html coded text is displayed properly?你能帮我更正这段代码,以便正确显示 html 编码的文本吗? Or, is there another way to do this?或者,还有其他方法可以做到这一点吗?

Note im using php+odbc+Microsoft SQL Server 2008 R2 (RTM).注意我使用的是 php+odbc+Microsoft SQL Server 2008 R2 (RTM)。

You can use the utf-8 encoding您可以使用 utf-8 编码

I needed to access the database from within one particular webhosting service.我需要从一项特定的虚拟主机服务中访问数据库。 Pages are UTF-8 encoded and data received by forms should be inserted into database without changing the encoding.页面采用 UTF-8 编码,表单接收的数据应插入数据库而不更改编码。 The database is also in UTF-8.数据库也是UTF-8。

Neither SET character set 'utf8' or SET names 'utf8' worked properly here, so this workaround made sure all variables are set to utf-8. SET 字符集 'utf8' 或 SET 名称 'utf8' 在这里都不能正常工作,因此此解决方法确保所有变量都设置为 utf-8。

  <?php

// ... (creating a connection to mysql) ...

mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'", $conn);

$re = mysql_query('SHOW VARIABLES LIKE "%character_set%";')or die(mysql_error());
while ($r = mysql_fetch_assoc($re)) {var_dump ($r); echo "<br />";} exit;

?>`

All important variables are now utf-8 and we can safely use INSERTs or SELECTs with mysql_escape_string($var) without any encoding functions.所有重要的变量现在都是 utf-8,我们可以安全地使用 INSERT 或 SELECT 和 mysql_escape_string($var) 而没有任何编码函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM