如何在json文件中插入现有的php字符串文字? [英] How can I interpolate an existing php string literal inside a json file?

查看:91
本文介绍了如何在json文件中插入现有的php字符串文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个php脚本,它通过使用file_get_contents()访问json文件,在json文件中,我们声明了一个php变量.请让我知道,有什么方法可以解析json文件中的php变量.

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.

下面是代码: test.json

Below is the code: test.json

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

用于访问json文件的php脚本

php script for accessing the json file

<?php
$result = 'Hello';
$event = file_get_contents('test.json');
echo $event;
?>

现在输出如下:

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

但是我需要这样的输出

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

我无法访问json文件中的$result变量. 任何帮助表示感谢.

I'm not able to access the $result variable inside a json file. Any help Appreciated.Thanks

推荐答案

我可能对此表示不满,但在这种情况下可以使用eval吗?

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;

输出:

{
    "result":"Hello world",
    "count":3
}

这篇关于如何在json文件中插入现有的php字符串文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆