Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-str_replace返回原始字符串_Php_Ajax - Fatal编程技术网

PHP-str_replace返回原始字符串

PHP-str_replace返回原始字符串,php,ajax,Php,Ajax,我正在构建一个简单的JS终端shell模拟器,它通过AJAX将命令发布到PHP。 请将安全性放在一边,这仅用于学习和演示目的。 现在我的问题是,str_replace()无法按预期工作,事实上,它返回的是未更改的输入字符串。它应该是这样工作的: 此主机的名称为$hostname-->是,此字符串包含一个变量-->用testserver替换$hostname-->返回此主机的名称为testserver 我做错了什么 这是我对echo和export的响应脚本: <? // get enviro

我正在构建一个简单的JS终端shell模拟器,它通过AJAX将命令发布到PHP。
请将安全性放在一边,这仅用于学习和演示目的。 现在我的问题是,str_replace()无法按预期工作,事实上,它返回的是未更改的输入字符串。它应该是这样工作的:
此主机的名称为$hostname
-->
是,此字符串包含一个变量
-->
用testserver替换$hostname
-->返回
此主机的名称为testserver

我做错了什么

这是我对
echo
export
的响应脚本:

<?
// get environment variables from JSON
$vars = json_decode(file_get_contents('environment.json'), true);

// get request params
$method = $_SERVER['REQUEST_METHOD'];
$action = $_POST['action'];
$data = $_POST['data'];

switch ($action) {

case 'echo':
    $cmd = $data;

        // if the string in question contains a variable, eg. "the time is $time"
        if (strpos($cmd,'$')) {
        $output = '';

        // for each environment variable as variable => value
        foreach ($vars as $var => $val) {

            // replace every variable in the string with its value in the command
            $output = str_replace($var,$val,$cmd);
        }
        echo $output;
    } else { 

        // if it does not contain a variable, answer back the query string 
        // ("echo " gets stripped off in JS)
        echo $cmd; 
    }
break;

case 'export':

    // separate a variable declaration by delimiter "="
    $cmd = explode('=',$data);

    // add a $-sign to the first word which will be our new variable
    $var = '$' . array_shift($cmd);

    // grab our variable value from the array
    $val = array_shift($cmd);

    // now append everything to the $vars-array and save it to the JSON-file 
    $vars[$var] = $val;
    file_put_contents("environment.json",json_encode($vars));
break;
}
更好地使用:

if (strpos($cmd,'$') !== false) {
然后,每个替换都将“第一个”数据作为其输入数据。你应该这样做:

    $output = $cmd;

    // for each environment variable as variable => value
    foreach ($vars as $var => $val) {

        // replace every variable in the string with its value in the command
        $output = str_replace($var, $val, $output);
    }

请显示environment.json的内容。如果在您使用stru replace时找不到原因,请发布您使用的组合
$var
$val
$cmd
。请注意
strop
,如果您搜索的单词位于第一位,它将返回
0
,而不是
false
,所以你应该使用
if(strpos($cmd,$)!=false
IMHO更明智的做法是创建一个单独的对象或闭包数组,并将键作为命令名进行寻址,而不是使用带有逻辑的开关。@Alexander,Sal00m,hd:environment.json包含
{“$hostname”:“testserver”,“$test”:“123”}
谢谢,解决了这个问题。但我不明白为什么,你能解释一下吗?那太好了……每次再次运行循环时,都会获取“初始”数据($cmd),并在其中替换。因此,每次循环时,都会丢失以前在上一个循环中所做的更改。