简体   繁体   English

如何回显json字符串的PHP?

[英]How do i echo php for json string?

i need to echo $account into $url so that the url/path is whatever i get from $account plus the extension json. 我需要在$ url中回显$ account,以便url / path是我从$ account加上扩展名json得到的。 I can't figure it out with the quotes. 我无法用引号弄清楚。 i tried single and double quotes but no luck. 我尝试单引号和双引号,但没有运气。 any ideas? 有任何想法吗?

<?php 
$account = $_POST['account'];
$url = 'echo $account.json'; //
$content = file_get_contents($url);
$json = json_decode($content, true);
?>

Like this? 像这样?

<?php 
$account = $_POST['account'];
$url = $account. '.json'; //
$content = file_get_contents($url);
$json = json_decode($content, true);
?>

the . . operator is used for string concatenation in php. 运算符用于php中的字符串连接。 This means take the value of $account and append the string .json to the end, and store that in the variable $url . 这意味着将$account的值附加到字符串.json的末尾,并将其存储在变量$url The rest of the code looks all right from there. 其余的代码从那里看起来还不错。 There are a few other ways to do this as well with strings in php, but I find this one simplest. 还有一些其他方法也可以用php中的字符串来做到这一点,但是我发现这是最简单的。

You can do this as this 你可以这样

$account = $_POST['account'];
$content = file_get_contents($account.'.json');
$json = json_decode($content, true);

Need I remind you how, vulnerable your codes are to injection? 我需要提醒您,代码容易受到攻击吗?

The documentation on strings goes into detail on how to format strings. 有关strings的文档详细介绍了如何格式化字符串。 You might want to try using curly braces ( {} ) around your variable as well to delineate where the identifier ends. 您可能要尝试在变量周围使用花括号( {} )来描绘标识符的结束位置。

$url = "{$account}.json"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM