json_decode works for strings that were recently encoded, but not from strings read from files?
<?
$ips=fopen("e","r+");
var_dump(fread($ips,
filesize('e'))); // Returns 'string(190) "[["37.19.199.140"]]"'
$aga=json_decode(fread($ips,filesize('e')));
echo ($aga); // Returns nothing. Why?
if(gettype($aga)!="array"){
$aga=[];
}
$aga[count($aga)]=[$_SERVER['REMOTE_ADDR']];
ftruncate($ips,0);
fwrite($ips,json_encode($aga));
fclose($ips);
echo '<div>';
var_dump(json_encode($aga)); // Returns 'string(19) "[["37.19.199.140"]]"'
echo '<div>';
var_dump(json_decode(json_encode($aga))); // Returns 'array(1) { [0]=> array(1) { [0]=> string(13) "37.19.199.140" } }'
?>
This is my code. Apparently the string in my file is 190 characters, but the string created within the script is 19 characters? They look the same when printed out. Why does json_decode work for the string created inside the script, but not the string from the file? Here is the output.
string(190) "[["37.19.199.140"]]"
string(19) "[["37.19.199.140"]]"
array(1) { [0]=> array(1) { [0]=> string(13) "37.19.199.140" } }`
I tried making an IP logger that logs IP as a JSON array inside of a file. I expected it to work simply, but instead apparently json_decode doesn't work for what was attained from the file.