I have a php script , it accessing a json file by using file_get_contents()
, inside a json file, we have declared a php variable. Please let me know is there any way to parse the php variable inside a json file.
Below is the code: test.json
{
"result":"$result",
"count":3
}
php script for accessing the json file
<?php
$result = 'Hello';
$event = file_get_contents('test.json');
echo $event;
?>
Now the output is like below:
{ "result":"$result", "count":3 }
But I need the output like this
{ "result":"Hello", "count":3 }
I'm not able to access the $result
variable inside a json file.
Any help Appreciated.Thanks
I might get downvoted for this, but could eval be used in this situation?
<?php
$json = '{
"result":"$result",
"count":3
}'; //replace with file_get_contents("test.json");
$result = 'Hello world';
$test = eval('return "' . addslashes($json) . '";');
echo $test;
Output:
{
"result":"Hello world",
"count":3
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With