PHP:如何从字符串转储中提取JSON字符串 [英] PHP: How to extract JSON strings out of a string dump

查看:830
本文介绍了PHP:如何从字符串转储中提取JSON字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个巨大的字符串转储,其中包含常规文本和JSON的混合.我想从字符串转储中分离/删除JSON对象,仅获取文本.

I have a huge string dump that contains a mix of regular text and JSON. I want to seperate/remove the JSON objects from the string dump and get the text only.

这里是一个例子:

This is some text {'JSON':'Object'} Here's some more text {'JSON':'Object'} Yet more text {'JSON':'Object'} Again, some text.

我的目标是获取一个看起来像这样的文本转储(基本上删除了JSON):

My goal is to get a text dump that looks like this (basically the JSON is removed):

This is some text Here's some more text Yet more text Again, some text.

我需要用PHP来完成所有这些工作.文本转储始终是随机的,JSON数据结构也是随机的(大多数数据嵌套在内部).转储可能以JSON开头,也可能不以JSON开头,并且在字符串转储中可能包含一个以上的JSON对象.

I need to do this all in PHP. The text dump is always random, and so is the JSON data structure (most of the it is deeply nested). The dump may or may not start with JSON, and it may or may not contain more than one JSON object within the string dump.

我尝试在字符串上使用json_decode,但结果最终显示为NULL

I have tried using json_decode on the string but the result ends up as NULL

Amal的答案确实接近我想要的答案(请参阅下面的第二条评论):

Amal's answer is really close to what I want (see the 2nd comment below):

$str = preg_replace('#\{.*?\}#s', '', $str);

但是,它根本不能消除嵌套对象;例如括号中包含的数据:[][{}]

However, it doesn't get rid of nested objects at all; e.g. data contained in brackets: [] or [{}]

对不起,我不是正则表达式专家.

Sorry, I'm not an expert in regex.

我意识到你们中的有些人可能需要我正在处理的字符串转储的更具体的例子;因此,我创建了一个要点(请注意,这不是静态数据;转储中的数据将始终是不同的;上面的示例只是简化了我正在使用的字符串):

I realized that some of you may need a more concrete example of the string dump I'm dealing with; therefore I've created a gist (please note that this is not static data; the data in the dump will always be different; my example above just simplifies the string I'm working with): https://gist.github.com/anonymous/6855800

推荐答案

我希望您发布使用JSON_decode进行尝试时使用的代码,但是,好吧...

I wanted you to post the code you used in your attempt using JSON_decode but oh well...

您可以对PHP中的嵌套括号使用递归正则表达式:

You can use a recursive regex for nested braces in PHP:

$res = preg_replace('~\{(?:[^{}]|(?R))*\}~', '', $text);

regex101演示(以蓝色突出显示的部分将被删除).

regex101 demo (The part highlighted in blue will be removed).

这篇关于PHP:如何从字符串转储中提取JSON字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆