如何将此字符串存储为mysql中的有效json(utf8_encode和htmlentities没有帮助)

时间:2015-06-19 15:17:39

标签: php mysql json

这是我试图存储的字符串

<div id="site-summary">
    <p>
        <strong>Welcome to Whiteley Helyar.</strong> We are independent, friendly and professional, always seeking to achieve the best for our clients. Whiteley Helyar is one of Bath’s leading residential estate agencies and deal only in sales – nothing else.  Our many satisfied customers value our efficiency, honesty and expert understanding of property in Bath and the surrounding villages. Not only will you find us knowledgeable and helpful, we are also eager and progressive in our approach. If you are house-hunting in one of the UK’s most beautiful and vibrant cities, our experienced team of partners and negotiators can help you find the perfect property.
    </p>
</div>

这里有一些不寻常的字符导致json_encode在数组中没有正确地将字符串编码为json。

这是我的代码,其中$ about_us是上面的字符串

$about_us = utf8_encode($about_us);
$about_us = htmlentities($about_us, ENT_QUOTES | ENT_IGNORE, "UTF-8");
$about_us = mysql_real_escape_string($about_us);

$link_array = array();
$link_array['stuff'] = 'other string';
$link_array['about_us'] =  $about_us;
$json= json_encode($link_array);

然后我将$ json存储在mysql文本字段中。然而。我可以从检查$ json字符串看到它是无效的json并在http://jsonlint.com/上失败。当我做一个json_decode的东西工作正常,但关于我们是空的。

这是about_us属性的数据库中的内容:

&lt;div id=&quot;site-summary&quot;&gt;\r\n             &lt;p&gt;&lt;strong&gt;Welcome to Whiteley Helyar.&lt;/strong&gt; We are independent, friendly and professional, always seeking to achieve the best for our clients. Whiteley Helyar is one of Bath&acirc;u0080u0099s leading residential estate agencies and deal only in sales &acirc;u0080u0093 nothing else.  Our many satisfied customers value our efficiency, honesty and expert understanding of property in Bath and the surrounding villages. Not only will you find us knowledgeable and helpful, we are also eager and progressive in our approach. If you are house-hunting in one of the UK&acirc;u0080u0099s most beautiful and vibrant cities, our experienced team of partners and negotiators can help you find the perfect property.&lt;/p&gt;\r\n           &lt;/div&gt;

请提出任何想法

编辑:这与链接的问题不同,因为utf8_encode在这里没有帮助(这是该问题的可接受答案)。

1 个答案:

答案 0 :(得分:0)

首先,如果您仍在使用mysql_real_escape_string,那么您可能没有使用PDO与数据库进行交互。你现在真的应该使用PDO。

如果您使用PDO,它可以解决您的许多问题,您可以做一些简单的事情:

$db = new PDO('mysql:host=CHANGE_THIS_TO_YOUR_HOST_NAME;
    dbname=CHANGE_THIS_TO_YOUR_DATABASE',
    'CHANGE_THIS_TO_YOUR_USERNAME',
    'CHANGE_THIS_TO_YOUR_PASSWORD');


$about_us = '<div id="site-summary">
            <p><strong>Welcome to Whiteley Helyar.</strong> We are independent, friendly and professional, always seeking to achieve the best for our clients. Whiteley Helyar is one of Bath’s leading residential estate agencies and deal only in sales – nothing else.  Our many satisfied customers value our efficiency, honesty and expert understanding of property in Bath and the surrounding villages. Not only will you find us knowledgeable and helpful, we are also eager and progressive in our approach. If you are house-hunting in one of the UK’s most beautiful and vibrant cities, our experienced team of partners and negotiators can help you find the perfect property.</p>
            </div>';

$link_array = array();
$link_array['stuff'] = 'other string';
$link_array['about_us'] =  $about_us;
$json= json_encode($link_array);

$query = $db->prepare("INSERT INTO settings 
    (json) VALUES (:json)
    ");
$query->bindValue(':json', $json, PDO::PARAM_STR);
$query->execute();

然后,如果你查看数据库并将json字段中的内容复制并粘贴到jsonlint中,它将验证就好了。另外,这样,html实际上在数据库中是可读的(而不是必须存储&lt;&quot;等所有内容。