php - Extract json from mixed content string -
i have string of mixed content, like:
{json stuff} other content same output has < html > tags < div >some< / div > , more stuff, @ end has {json stuff}
so need able use 3 elements:
first json this:
$first_json = json_decode(substr($mixed_string,0, strpos($mixed_string,"}")+1 )); // me first json content // need second json @ end of string, // length of string unknown , json length unknown // after do: echo preg_replace("/\{[^)]+\}/","",$mixed_string); // , html string...
so, how can last json?
you can use regex grab json values, , it'll let grab json out of string, no matter length. let's have div
id of content, , div
has json want extract:
edit: added extraction process function.
function extractjson(content) { var regex = /({.*?})/g, raw = content.match(regex), output = [], errors = []; raw.foreach(function(el) { try { output.push(json.parse(el)); } catch (e) { errors.push('issue object: ' + el); } }); return { results: output, errors: errors } } // grab elements contain json var elements = document.getelementbyid('content').innerhtml; console.log(extractjson(elements));
Comments
Post a Comment