How can I interpolate an existing php string literal inside a json file?

前端 未结 4 1908
一生所求
一生所求 2021-02-13 02:39

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 w

4条回答
  •  北荒
    北荒 (楼主)
    2021-02-13 03:25

    Instead of doing it your way like { "result":"$result", "count":3 }

    I would have done like this.

    I would have specified a short code for the variable for this

    { "result":"**result**", "count":3 }

    and when I will get that JSON in PHP, just replace that with my desired PHP variable

    $event = file_get_contents('test.json');
    
    var $result = "hello";
    $parsed_json = str_replace("**result**",  $result,$event  ); 
    

提交回复
热议问题