regex - PHP: How to extract JSON strings out of a string dump -
i have huge string dump contains mix of regular text , json. want seperate/remove json objects string dump , text only.
here example:
this text {'json':'object'} here's more text {'json':'object'} yet more text {'json':'object'} again, text.
my goal text dump looks (basically json removed):
this text here's more text yet more text again, text.
i need in php. text dump random, , json data structure (most of nested). dump may or may not start json, , may or may not contain more 1 json object within string dump.
i have tried using json_decode
on string result ends null
edit: amal's answer close want (see 2nd comment below):
$str = preg_replace('#\{.*?\}#s', '', $str);
however, doesn't rid of nested objects @ all; e.g. data contained in brackets: []
or [{}]
sorry, i'm not expert in regex.
i realized of may need more concrete example of string dump i'm dealing with; therefore i've created gist (please note not static data; data in dump different; example above simplifies string i'm working with): https://gist.github.com/anonymous/6855800
i wanted post code used in attempt using json_decode
oh well...
you can use recursive regex nested braces in php:
$res = preg_replace('~\{(?:[^{}]|(?r))*\}~', '', $text);
regex101 demo (the part highlighted in blue removed).
Comments
Post a Comment