I have a PHP file that runs a Perl script, using popen and the perl script outputs the following back to the php
{ 'City' => [ 'LA', 'Chicago', 'NY' ], 'Name' => 'Kevin Bridges', 'Id' => '7075', 'Last-Status-Change' => { 'Time' => 14172911, 'User' => 'kbridge', 'To' => 'LAX', 'From' => 'ORD' }}
I cannot modify the perl script, and I really don't know the contents of it. But it looks like it is outputting JSON. I have tried using json_encode to grab the contents of the output but no success. Can anyone tell me if it is possible to parse this or do I have to manually write a parser?
That's not JSON but is "almost" the PHP []
array syntax, except it uses some {}
. You could try:
eval('$array = ' . str_replace(['{','}'], ['[',']'], $output) . ';');
print_r($array);
It does not looks like to be a valid JSON, it seems to be a PERL hash, so i think you gonna need to parse that manually... a simple way to you accomplish that in PHP, it would be to replace the {-} to [] and you can eval that string to be consider an array in PHP
Convert =>
to :
and '
to "
. After that use json_decode
to create associative array from the string.
$array = json_decode(str_replace(["=>", "'"], [":", '"'], $a), true);