用其他内容PHP替换字符串内容

时间:2011-08-05 11:17:43

标签: php string

我在PHP脚本的顶部声明了一个字符串:

$dirname = "./rootfs/home/".$user."/";

运行代码后,该字符串应该被替换,所以它看起来像这样:

$dirname = "./rootfs/home/".$user."/testdir/";

有没有办法更换内容?这肯定是一个愚蠢的问题,但没有找到任何办法,谢谢! :d

我正在使用的代码: 主要PHP:

<?php

//Variables
$rootfs = "rootfs/";
$user = "www";
$dirname = "./rootfs/home/".$user."/";

//Variables

//Commands on rootfs/bin/
include($rootfs."/bin/ls.php");
include($rootfs."/bin/about.php");
include($rootfs."/bin/echo.php");
include($rootfs."/bin/uname.php");
include($rootfs."/bin/apt-get.php");
include($rootfs."/bin/whoami.php");
include($rootfs."/bin/cd.php");
//Commands on rootfs/bin/

$command=$_POST['command'];

$commandexp = explode(" ", $command);

switch ($commandexp[0]) {
case "cd":
    //$dirname = "./rootfs/home/".$user."/";
    //$prompt = cd($dirname, $commandexp[1]);
    $dirname .= cd($dirname, $commandexp[1]);
    echo $dirname;
    break;
case "ls":
    switch ($commandexp[1]) {
    case "-la":
        //$dirname = "./rootfs/home/".$user."/";
        $files = ls($dirname);
        foreach($files as $value){
            $prompt .= substr(sprintf('%o', fileperms($dirname.$value)), -4)."   ".$user."   ".$value."<br>";
        }
        break;
    case "--help":
        $prompt = "Usage: ls [-la]";
        break;
    default:
    echo $dirname." ";
        $files = ls($dirname);
        foreach($files as $value){
            $prompt .= $value." ";
        }
        break;
    }
    break;
default:
    $prompt = "command not found";

}

echo $prompt;

?>

cd.php

<?php
function cd($actualdir, $dir) {
    //echo $actualdir.$dir."<br>";
    if(file_exists($actualdir.$dir)) {
        echo $dir;
        return $dir;
    } else {
        return "no";
    }
}

?>

ls.php

<?php
function ls ($directory) {

    // create an array to hold directory list
    $results = array();

    // create a handler for the directory
    $handler = opendir($directory);
    echo $handler;
    // open directory and walk through the filenames
    while ($file = readdir($handler)) {

        // if file isn't this directory or its parent, add it to the results
        if ($file != "." && $file != "..") {
            $results[] = $file;
        }

    }

    // tidy up: close the handler
    closedir($handler);

    // done!
    return $results;

}

?>

4 个答案:

答案 0 :(得分:2)

要添加字符串,请执行以下操作:

$dirname .= "/testdir/";

答案 1 :(得分:1)

将一个字符串连接到另一个字符串的末尾:$dirname .= 'testdir/'
要将全新的字符串重新分配给变量:$dirname = 'This is the new string'
String Operators

答案 2 :(得分:1)

这就是你想要的:

$dirname .= "testdir/";

答案 3 :(得分:0)

最后使用会话变量来存储实际的目录