PHP 包含 json 数据的文本的正则表达式

PHP regular expression for text with json data

我正在寻找 PHP 正则表达式来匹配文本字符串中多组 json 数据的模式。

Paragraph One text

[[{"fid":"28","view_mode":"teaser","fields":{},"type":"media","attributes":{"class":"media-element file-teaser"},"link_text":null}]]

Second Paragraph

[[{"fid":"26","view_mode":"preview","fields":{},"type":"media","attributes":{"class":"file media-element file-preview"},"link_text":"mohan sample.mp4"}]]

以上是带有 json 个样本的示例文本,我想从每个 json 数据中提取 "fid" 的值。

有什么建议吗?

\[\[{(?:(?!\]\])[\s\S])*"fid":"\K\d+

您可以尝试 this.See 演示。

https://www.regex101.com/r/rG7gX4/10

$re = "/\[\[{(?:(?!\]\])[\s\S])*\"fid\":\"\K\d+/mi";
$str = "[[{\"fid\":\"28\",\"view_mode\":\"teaser\",\"fields\":{},\"type\":\"media\",\"attributes\":{\"class\":\"media-element file-teaser\"},\"link_text\":null}]]\n\nSecond Paragraph\n\n[[{\"fid\":\"26\",\"view_mode\":\"preview\",\"fields\":{},\"type\":\"media\",\"attributes\":{\"class\":\"file media-element file-preview\"},\"link_text\":\"mohan sample.mp4\"}]]";

preg_match_all($re, $str, $matches);