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.
Here is an example:
This is some text {'JSON':'Object'} Here's some more text {'JSON':'Object'} Yet more text {'JSON':'Object'} Again, some text.
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.
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.
I have tried using json_decode
on the string but the result ends up as NULL
EDIT: 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