I am stuck in a problem that I have a string which has a complete description as well as a json containing some Ids. How can I take the Json out the String and perform any event on it..
My data looks like following
The description of xxxxxxxxxxxxxxxxxxxxxxxxxx
[{"id":"613"},{"id":"614"},{"id":"615"}]
Is there any way that I can have the Complete Description and also have the IDs so that I can decode them and use where I want?
Thank you in advance for the support
try this
$string = 'The description of xxxxxxxxxxxxxxxxxxxxxxxxxx [{"id":"613"},{"id":"614"},{"id":"615"}] asdasd';
if(false !== preg_match('/\[(.*)\]/', $string, $matches)) {
for($n = 1; $n < count($matches); $n++) {
$json_result = json_decode('['.$matches[$n].']', true);
if(null === $json_result) {
//cannot parse json
}
print_r($json_result);
}
}
This will find the first LINE which has JSON code if your json is in new line !!!
$string = 'The description of xxxxxxxxxxxxxxxxxxxxxxxxxx
[{"id":"613"},{"id":"614"},{"id":"615"}]';
$strings = explode("
",$string);
foreach($strings as $str){
$json = json_decode($str,TRUE); //TRUE for array responde
if(!empty($json)){
break;
}
}
var_dump($json);