如何str_replace变量$ name到文件

时间:2013-01-24 15:14:06

标签: php str-replace

当我尝试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";
    }
    ?>

这是我的问题我只需打印变量名称。我该怎么做?

1 个答案:

答案 0 :(得分: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;