0

当我尝试str_replace输入文件名时,fff.php我无法将$var名称打印到文件中。

<?PHP
$path_to_file = 'fff.php';
$var_str = var_export($viewer, true);
$file_contents = file_get_contents($path_to_file);
$file_contents = str_replace("//","case \"13\";
$viewer =\"jjhj/jhjh/344.mp3\";  //I put value to variable 
echo $viewer ; //Here I need to print the $ and name of the variable only
break;
//",$file_contents);
file_put_contents($path_to_file,$file_contents);
echo "your file is ok";
?>

看看当我尝试替换变量的名称而不是值时,看看结果会发生什么 -

fff.php

<?php
$id=$_GET['id'];
switch ($id){
case "1";
$viewer = "jhhghg/pic1.jpg";
echo $viewer ;
break ;
// my str_replace starting
    case "13";
     ="jjhj/jhjh/344.mp3";  //I put value to variable but now name empty
    echo  ; //Here I need to print the $ and name of the variable only  also here
    break;
    //
    //my str_replace end
    default;
    echo"your request error";
    }
    ?>

那是我的问题,我只需要打印变量名。我怎么做?

4

1 回答 1

1

您是否忘记转义“$”符号($viewer)?转义它们,否则 php 将其假定为变量并将其替换为它的值,该值不存在。

$file_contents = str_replace("//","case \"13\";
\$viewer =\"jjhj/jhjh/344.mp3\";  //I put value to variable 
echo \$viewer ; //Here I need to print the \$ and name of the variable only
break;
于 2013-01-24T15:24:33.097 回答