如何使用php正则表达式替换json键值


How to replace json key value using php regular expression?

我有一个无效的json字符串,如下所示:

{
   "cat":{
      "id":"1234567",
      "source_id":null,
      "title_en":"first season",
      "description_en":"This is spring category "
   },
   "videos":[
      {
         "id":"312412343",
         "url":"'/2015-07-17'/1abcd.mp4",
         "img":"image'/44'/'/2015-07-17'/1abcd.jpg",
         "description1":"some text for" description here",
         "description2":"some text for" description here2",
         "title":"first title here"
      },
      {
         "id":"2342343",
         "url":"'/2015-07-16'/2dcdeg.mp4",
         "img":"images'/44'/'/2015-07-16'/2dcdeg.jpg",
         "description1":"some other text here" for description ",
         "description2":"some other text here" for description2 ",
         "title":"second title here"
      }
   ]
}

我想使用正则表达式将description1和description2的值更改为单词空。有人能帮助我如何使用正则表达式来实现这一点吗?谢谢

     "description1":"empty",
     "description2":"empty",

试试这个代码:

$result = json_decode($json, true);
array_walk($result['videos'], function (&$item) {$item['description1'] = 'empty'; $item['description2'] = 'empty';});
$json = json_encode($result);
print_r($json);

使用正则表达式的解决方案:

print_r(preg_replace('/'"description(1|2)'"':'".*?'"',/s', '"description$1":"empty",',$json));
    $json = '{
   "cat":{
      "id":"1234567",
      "source_id":null,
      "title_en":"first season",
      "description_en":"This is spring category "
   },
   "videos":[
      {
         "id":"312412343",
         "url":"'/2015-07-17'/1abcd.mp4",
         "img":"image'/44'/'/2015-07-17'/1abcd.jpg",
         "description1":"some text for description here",
         "description2":"some text for description here2",
         "title":"first title here"
      },
      {
         "id":"2342343",
         "url":"'/2015-07-16'/2dcdeg.mp4",
         "img":"images'/44'/'/2015-07-16'/2dcdeg.jpg",
         "description1":"some other text here" for description ",
         "description2":"some other text here" for description2 ",
         "title":"second title here"
      }
   ]
}';
$json = preg_replace('/(description'd+":)"(.*)"/', '$1"empty"', $json);
echo '<pre>' . $json . '</code>';
$json = json_decode($json, true);
var_dump($json);

演示

您不需要正则表达式。您可以使用以下代码行:

$str = <<<'json_str'
{
   "cat":{
      "id":"1234567",
      "source_id":null,
      "title_en":"first season",
      "description_en":"This is spring category "
   },
   "videos":[
      {
         "id":"312412343",
         "url":"'/2015-07-17'/1abcd.mp4",
         "img":"image'/44'/'/2015-07-17'/1abcd.jpg",
         "description1":"some text for description here",
         "description2":"some text for description here2",
         "title":"first title here"
      },
      {
         "id":"2342343",
         "url":"'/2015-07-16'/2dcdeg.mp4",
         "img":"images'/44'/'/2015-07-16'/2dcdeg.jpg",
         "description1":"some other text here for description ",
         "description2":"some other text here for description2 ",
         "title":"second title here"
      }
   ]
}
json_str;
$arr = json_decode($str);
$arr['videos']['empty'] = $arr['videos']['description1'] ;
unset($arr['videos']['description1']);
$converted_to_json = json_encode($arr);

在这些行中,我使用json_decode()函数将json字符串转换为数组。然后,我将数组中的description1键替换为空,并使用json_encode()函数将其再次转换为json字符串。

我希望我的回答对你有用。