我觉得你很困惑。您想删除字符串外的双引号,但这实际上只是字符串的定义。
如果您简单地使用echo
的值,将会看到您想要删除的双引号始终不存在。
Json
通常作为字符串处理,因此应将其括在引号中。重要的是要使用json的格式。
,
检查此内容,
您可以json_decode,然后使用JSON_UNESCAPED_SLASHES
进行编码
$p = "{\"match\":\"https:\/\/mong.com\/sddaad\",\"type\":\"exact\",\"activated\":\"True\"}";
echo json_encode(json_decode($p),JSON_UNESCAPED_SLASHES);
Demo
您可以毫无问题地将输出保存到数据库,并且可以使用json_decode读取它们。
输出
{"match":"https://mong.com/sddaad","type":"exact","activated":"True"}
,
我不确定您要实现的目标是什么,但您可能想看看trim
方法
$string = "sample text"
trim($string,'"')
trim
删除字符串末尾的指定字符。如果未指定表达式,则会删除空格
,
您可以使用PHP函数addlashes()使其正常工作。
<?php
$json_array = array(
'title' => 'I love Dexter series this is a test\'s with "special" characters'
);
echo $json_decode =addslashes(json_encode($json_array));
?>
输出:{\“ title \”:\“我爱德克斯特系列,这是一个带有\\”特殊\\“字符\”}的测试\
,
$ p = preg_replace('/“([[a-zA-Z] + [a-zA-Z0-9 _] *)”:/','$ 1:',$ p);
,
要将其保存在laravel中的json字段中,您需要做的是获取您的字符串并将其解码为数组,然后将属性值设置为该数组。
$p = "{\"match\":\"https:\/\/mong.com\/sddaad\",\"activated\":\"True\"}"
$value = json_decode($p,true); //second parameter to get the result as an associative array
$user->properties = $value;
$user->save();
不要忘记在模型类中设置该属性的转换
class User extends Model
{
protected $casts = [
'properties' => 'array'
];
本文链接:https://www.f2er.com/3161049.html