php - PHP/Regex: Parse JSON in string
问题描述
I'm trying to find simple key-value-pairs in strings, given as JSON-objects, while using preg_replace_callback().
Unfortunately, the values given can be of type string, number, boolean, null, array - and worst of all - objects. My own attempts solving this problem resulted in either an incomplete selection or over-selecting multiple JSON occurances as one.
Here the things i tried:
String:
text text {"key":{"key":"value"}} text
Regex:
\{"(.+?)"\:(.+?)\}
Match:
{"key":"value"
Above: This ignores the inner }-bracket
String:
text text {"key":{"key":"value"}} text
Regex:
\{"(.+?)"\:(.+)\}
Match:
{"key":"value"}
Above: This would (theoretically) work, but when having multiple JSON occurances, i get:
{"key":"value"}} {"key":{"key":"value"}
Next attempt:
String:
text text {"key":{"key":"value"}} {"key":{"key":"value"}} text
Regex:
\{"(.+?)"\:(?:(\{(?:.+?)\})|(?:")?(.+?)(?:")?)\}
Match:
{"key":"value"}
Above: Again, that would theoreticcally work. But when taking, for example, the following string:
text text {"key":{"key":{"key":"value"}}} text
The result is...
{"key":{"key":"value"}
Missing one bracket
解决方案
PCRE supports recursive matching for that kind of nested structures. Here is a demo:
$data = 'text text
{"key":{"key":"value{1}","key2":false}}
{"key":{"key":"value2"}}
{"key":{"key":{"key":"value3"}}} text';
$pattern = '(
\{ # JSON object start
(
\s*
"[^"]+" # key
\s*:\s* # colon
(
# value
(?:
"[^"]+" | # string
\d+(?:\.\d+)? | # number
true |
false |
null
) |
(?R) # pattern recursion
)
\s*
,? # comma
)*
\} # JSON object end
)x';
preg_replace_callback(
$pattern,
function ($match) {
var_dump(json_decode($match[0]));
},
$data
);
推荐阅读
- php - 如何根据条件显示消息 - 使用 laravel
- javafx - JavaFX| TableView 未填充某些字段
- javascript - 为什么这个 JS 函数在加载时不起作用?
- vb.net - 从列表框中删除特定项目 - vb.net
- amazon-web-services - 用 DynamoDB 中的新项目替换旧项目
- loops - 如何在c#中重复一个动作来修复跳跃机制
- android - 我绝对必须在我的存储中恢复文本消息
- asp.net-mvc - 如何在循环中设置 DropDownListFor 的默认值?
- javascript - 自定义表情反应在某些平台上以错误的顺序出现
- python - psycopg2 fetchone() 方法返回一个包含表示 «result» 元组的字符串的单元素元组