【问题标题】:How to replace json key value using php regular expression? [duplicate]如何使用 php 正则表达式替换 json 键值? [复制]
【发布时间】:2016-09-02 23:58:57
【问题描述】:

我有一个像这样的无效 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",

【问题讨论】:

  • 让它变得简单。解码您的 json,更改数组值,然后再次对其进行编码。
  • 感谢您的回复。我的 JSON 并非一直有效(有时它在 description1 和 description2 中有双引号)。如果您建议的方法适用于无效的 JSON,我不知道,所以我会更喜欢正则表达式。
  • 那你解决了错误的问题。如果你期望 json 你应该得到有效的 json,否则你做错了。如果您需要使用正则表达式来解决它,它不会使问题变得更好。
  • 我使用的 api 给了我无效的 json,所以我不能做任何事情,除非我自己修复 json,以某种方式使 descrption2 和 description 1 为空!但不知道怎么做!
  • 那么 API 就是垃圾。但请确保在您的问题中包含您有时有 invliad json 的信息。还可以看看regex101.com 来玩并构造一个正则表达式:)

标签: php json regex preg-replace preg-match-all


【解决方案1】:

试试这个代码:

$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));

【讨论】:

  • 我试过你的解决方案,我得到了 {"videos":null} !我的 json 在描述 2 和描述 1 中有双引号!
  • 非常感谢您更新的解决方案有效,但如果描述跨越多行(unicode 和非 unicode 的混合),那么它会失败!如果 description2 和 descriptitoin1 值很长(不在在线线上),有什么方法可以让您的 preg_replace 工作?
  • 答案已更新@user1788736
  • 我可以知道您为什么使用 \:.*?\,/s 而不是 \:.*?\"\,/s 吗?就我而言,第二种方法有效!(替换了整个描述值我想要的单词是空的!:-)非常感谢。
  • @user1788736 按照您的建议,使用 *?\"\ 更正确。
【解决方案2】:
    $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);

Demo

【讨论】:

    【解决方案3】:

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

    $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 字符串。

    希望我的回答对你有用。

    【讨论】:

    • 感谢您的回复。如果我得到无效的 json,例如如果我在 description1 和 description2 中有双引号,你的方法是否有效?
    猜你喜欢
    • 2014-03-29
    • 1970-01-01
    • 2013-12-09
    • 2011-02-21
    • 1970-01-01
    • 2013-12-05
    • 1970-01-01
    • 2014-10-27
    • 2021-12-07
    相关资源
    最近更新 更多