【问题标题】:Get a certain value in a JSON string using a regex使用正则表达式获取 JSON 字符串中的某个值
【发布时间】:2014-08-06 00:54:04
【问题描述】:

如何使用 PHP 正则表达式提取特定值并将其放入如下变量中:

$output = 'testing tag speaker on left';

这就是我要解析的变量的样子:

$test ='
\"sticky\": {
    \"type\": \"area\",
    \"positionLeft\": \"23.90625\",
    \"positionTop\": \"19.6875\",
    \"id\": \"false\",
    \"allowToModify\": \"true\"
},
\"area\": {
    \"width\": \"95.625\",
    \"height\": \"170.625\",
    \"caption\": \"testingtagspeakeronleft\"
},
\"spot\": {
    \"bubbleDirection\": \"top\",
    \"title\": \"\",
    \"content\": \"\",
    \"contentRaw\": \"\",
    \"size\": \"0\",
    \"color\": \"white\",
    \"urlSpot\": \"\",
    \"urlSpotTarget\": \"_self\",
    \"urlGoogle\": \"\",
    \"urlYouTube\": \"\",
    \"urlVimeo\": \"\",
    \"urlWikipedia\": \"\",
    \"urlFacebook\": \"\"
}
';

【问题讨论】:

  • 你试过json_decode吗?
  • 你从哪里得到这些数据?
  • @thg435 我对此并不熟悉,但我需要使用 php 来完成
  • @hek2mgl 它来自 wp-stickies
  • 这看起来像一个 JSON 字符串。您可以使用json_decode 对其进行解码。如果您不知道 JSON 是什么,请尝试查找。

标签: php regex json


【解决方案1】:

关于使用正则表达式解析的所有免责声明,如果你真的需要,这个正则表达式会得到你的标签:

\\"caption\\":\\"\K.*?(?=\\"})

See demo

解释正则表达式:

  \\                    # '\'
  "caption              # '"caption'
  \\                    # '\'
  ":                    # '":'
  \\                    # '\'
  "                     # '"'
  \K                    # '\K' (resets the starting point of the
                        # reported match)
  .*?                   # any character except \n (0 or more times)
  (?=                   # look ahead to see if there is:
    \\                  # '\'
    "}                  # '"}'
  )                     # end of look-ahead

【讨论】:

  • 我只是好奇。这和 HTML 有什么关系?
  • @AmalMurali 哈哈,没什么。只是打字太快了。感谢您的提醒。并且为您的答案 +1,您处理了非正则表达式的答案真是太棒了。 :)
【解决方案2】:

最好的做法是修复数据源。那么你根本不必担心字符串操作。 总是 生成有效的 JSON。然后你可以简单地使用json_decode() 来处理它。

这是一个 JSON 字符串,但带有转义的斜杠。要将其转换为更可用的形式,您可以使用stripslashes(),然后通过将字符串包裹在一对花括号 ({...}) 中手动将其转换为 JSON 格式。

一旦字符串是有效的 JSON,您可以简单地使用json_decode()(第二个参数设置为true)将其解码为关联数组:

$json = '{'.stripslashes($test).'}';
$arr  = json_decode($json, true);

var_dump($arr['area']['caption']);

输出:

string(27) "testing tag speaker on left"

Demo

【讨论】:

    【解决方案3】:

    数据是几乎 json。你需要用花括号把它包起来:

    // Use double quotes
    $test ="\"sticky\":{\"type\":\"area\",\"positionLeft\":\"23.90625\",\"positionTop\":\"19.6875\",\"id\":\"false\",\"allowToModify\":\"true\"},\"area\":{\"width\":\"95.625\",\"height\":\"170.625\",\"caption\":\"testing tag speaker on left\"},\"spot\":{\"bubbleDirection\":\"top\",\"title\":\"\",\"content\":\"\",\"contentRaw\":\"\",\"size\":\"0\",\"color\":\"white\",\"urlSpot\":\"\",\"urlSpotTarget\":\"_self\",\"urlGoogle\":\"\",\"urlYouTube\":\"\",\"urlVimeo\":\"\",\"urlWikipedia\":\"\",\"urlFacebook\":\"\"}";
    
    // Wrap in curly braces
    $data = json_decode('{' . $test . '}');
    
    // Obtain desired field    
    echo $data->area->caption;
    

    但是,最好在创建数据的地方解决这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-13
      • 1970-01-01
      • 2023-03-23
      • 2019-08-06
      • 2018-03-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多