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

Php 用其他内容替换字符串内容,php,string,Php,String,我在PHP脚本的顶部声明了一个字符串: $dirname = "./rootfs/home/".$user."/"; 运行代码后,应替换该字符串,如下所示: $dirname = "./rootfs/home/".$user."/testdir/"; 有没有办法替换内容?这当然是一个愚蠢的问题,但没有找到任何方法,谢谢!:D 我正在使用的代码: 主要PHP: <?php //Variables $rootfs = "rootfs/"; $user = "www"; $dirname

我在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;

}

?>

将一个字符串连接到另一个字符串的末尾:
$dirname.='testdir/'

为变量重新分配一个全新的字符串:
$dirname='这是新字符串'


请参见

将一个字符串连接到另一个字符串的末尾:
$dirname.='testdir/'

为变量重新分配一个全新的字符串:
$dirname='这是新字符串'


请参见

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

$dirname .= "/testdir/";

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

$dirname .= "/testdir/";
这就是你想要的吗:

$dirname .= "testdir/";
这就是你想要的吗:

$dirname .= "testdir/";


最后使用了一个会话变量来存储实际目录

最后使用了一个会话变量来存储实际目录

是的,我试过了,但后来回显时,字符串似乎是一样的:/Added main files,check out PHP是否到达
cd
函数行?
cd
的返回值是多少?./rootfs/home/www/testdir/这就是写入
cd testdir
cd
返回的值。但是当写入
ls
时,它返回旧字符串的内容always@zad0xsis:这是因为在编写
ls
时,PHP代码不会更改
$dirname
的值。是的,我尝试过,但稍后回显时,字符串似乎相同:/Added main files,check out PHP是否到达
cd
函数行?
cd
的返回值是多少?./rootfs/home/www/testdir/这就是写入
cd testdir
cd
返回的值。但是当写入
ls
时,它返回旧字符串的内容always@zad0xsis:这是因为在编写
ls
时,PHP代码不会更改
$dirname
的值。是的,我尝试过,但稍后回显时,字符串似乎相同:/Added main files,check out是的,我尝试过,但稍后回显时,字符串似乎相同:/Added main files,check out是的,我试过了,但稍后回显时,字符串似乎相同:/yes我试过,但稍后回显时,字符串似乎相同:/