从php中只有一个json键值对删除额外的反斜杠

时间:2016-02-04 08:38:59

标签: php mysql json

这是从mysql表生成的小部分JSON。

{
  "id":"2",
  "project_title":"Jade Heights Tower II",
  "project_description":"\\u2022first one\\n\\u2022second one edited\\n",
  "latitude":"10.019615",
  "longitude":"76.339418",
  "property_type":" Luxury Apartments",
  "property_city":" Kakkanad",
  "property_status":"to-sell",
  "image_url":"http:\/\/zama.in\/miradmin\/assets\/images\/5d568e8ad9.jpg"
}

在此我只想从project_description删除额外的反斜杠  标签

当前

"project_description":"\\u2022first one\\n\\u2022second one edited\\n"

预期:

"project_description":"\u2022first one\n\u2022second one edited\n"

这是尝试过的代码。

$result = $connect->prepare("SELECT * FROM `mir_projects` ORDER BY `id`");
    $result->execute();
    if($result->rowCount()){

        $output = $result->fetchAll(PDO::FETCH_ASSOC);
        echo json_encode($output);
    }

1 个答案:

答案 0 :(得分:2)

首先,使用json_decode($ str,true)然后

解码json

试试这个,

<?php
$str="\\u2022first one\\n\\u2022second one edited\\n";
$newdescription = preg_replace('/\+/', "/\/", $str);
print_r($newdescription);
?>