2014-09-23 118 views
1

我想将数组转换为JSON。我的问题是,数组中的元素之一包含单引号,双引号等如何将数组转换为包含PHP中双引号的json?

<textarea name="description" id="description"> 
    Tesla is the living definition of bad*** and mad scientist. 
    During Tesla's later years, he made claims concerning a 
    "teleforce" weapon after studying the Van de Graaff generator. 
    The press called it a "peace ray" or death ray. Tesla described 
    the weapon as being able to be used against ground-based infantry 
    or for antiaircraft purposes. 
</textarea> 

当我提交我:

$description = $_POST['description']; 
json_encode($description); 

当我对其进行解码,我只是其中的一部分字符串。

+0

哪里是完整的代码? – Ghost 2014-09-23 03:05:06

+0

addslashes()http://php.net/manual/en/function.addslashes.php将它包裹在你的$描述中应该解决这个问题。 – mschuett 2014-09-23 03:08:07

+0

好的,当我使用addslashes()和retive从数据库中保存这个值时,会跳过apper? – user3054209 2014-09-23 03:11:03

回答

0

或者,也许你可以尝试addcslashes,这是更适应(你可以定义一个参数列表逃脱)。

+0

好的,当我使用addslashes()和retive从数据库中保存这个值时,会跳过apper? - – user3054209 2014-09-23 03:57:44

+0

当然他们会,但是你应该能够在[stripslashes](http://php.net/manual/fr/function.stripslashes.php)(或者在这种情况下是stripcslashes)后删除它们。但为什么你想要将JSON保存在数据库中? – 2014-09-23 06:59:51

+0

我想将它们保存在cookie中,当用户填写大量表单时,它将在15分钟后使用。然后从cookie中取出这些值并保存在数据库中 – user3054209 2014-09-23 13:39:25