I am sending request to remote server and it is sending plain text JSON type response! I tried to take that response in php variable and tried for json_decode but it always returns null value!
<?php
function removefunction($data){
checkagain:
$functionposition=stripos($data,"function()");
if($functionposition){
$subdata= substr($data, $functionposition);
$functiondata=substr($data, $functionposition,stripos($subdata,"}")+1);
$endoffunction=stripos($subdata,"}");
$endoffunction=$endoffunction+$functionposition;
$questionmarkpos=stripos($functiondata,'?"');
$colonpos=stripos($functiondata,'":"');
$realvalue=substr($functiondata, $questionmarkpos+2,$colonpos-$questionmarkpos-2);
$data=str_ireplace($functiondata,"\"$realvalue\"",$data);
goto checkagain;
}
return $data;
}
$json=<<<EOT
obj1431027525490 = { trains: [ { trainNo: "12392", startDate: "6 May 2015", trainName: "SHRAMJEEVI EXPRESS", trnName:function(){return _LANG=="en-us"?"SHRAMJEEVI EXPRESS":""}, divertedFrom: "NDLS", divertedTo: "GZB", trainSrc: "NDLS", trainDstn: "RGD", trainType: "SUPERFAST" }, { trainNo: "13162", startDate: "7 May 2015", trainName: "BLGT-KOLKATA EXP.", trnName:function(){return _LANG=="en-us"?"BLGT-KOLKATA EXP.":""}, divertedFrom: "NFK", divertedTo: "KOAA", trainSrc: "BLGT", trainDstn: "KOAA", trainType: "MAIL_EXP" }] };
EOT;
$json= substr($json, 19);
$json=substr_replace($json, "", -2);
echo $json."<br/><br/><br/>".PHP_EOL.PHP_EOL.PHP_EOL;
$json=removefunction($json);
$json = preg_replace('/(?<!")(?<!\w)(\w+)(?!")(?!\w)/', '"$1"', $json);
echo $json;
$contents = utf8_encode($json);
$obj = json_decode($json,true);
var_dump($obj);
?>
Issue: preg_replace adds quote between already quoted text!
PS: This is how i want to print the data into table via php array https://i.stack.imgur.com/Pf0Ek.png
You can use Diverted Train response from original website in above screenshot!