教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 PHP与命令行交互(实现查找和替换)

PHP与命令行交互(实现查找和替换)

发布时间:2016-10-28   编辑:jiaochengji.com
教程集为您提供PHP与命令行交互(实现查找和替换)等资源,欢迎您收藏本站,我们将为您提供最新的PHP与命令行交互(实现查找和替换)资源

<?php教程
// Ask for Input
fwrite(STDOUT, "Please Select Option(Default is Find) [1]Find [2]Replace Please Input Number: ");

// Get Input
$todo = trim(fgets(STDIN));
if(empty($todo)) $todo =1;
if($todo != 1 && $todo !=2){
    echo "Selected Error! ";
    exit;
}
if($todo==1){
    fwrite(STDOUT,"Please Input Find Directory(Default is Current Directory):");
    $dir = trim(fgets(STDIN));
    if(empty($dir)){
        $dir = getcwd();//当前目录
    }else{
        if(!is_dir($dir)){
            echo "Directory Not Exist! ";
            exit;
        }
    }
    fwrite(STDOUT,"Please Input Content of the Find:");
    $search = trim(fgets(STDIN));
    echo "In Directory'".$dir."'Find'".$search."',Please Wait... ";
    exec("find ".$dir." -exec grep --exclude='*.svn/*' -- '".$search."' {} ",$output);
    foreach($output as $val){
        echo "$val ";
    }
}else{// write input back
    fwrite(STDOUT, "Please Input Find Directory(Default is Current Directory):");
    $dir = trim(fgets(STDIN));
    if(empty($dir)){
        $dir = getcwd();//当前目录
    }else{
        if(!is_dir($dir)){
            echo "Directory Not Exist! ";
            exit;
        }
    }
    fwrite(STDOUT,"Please Input Prefix(Default is php):");
    $ext = trim(fgets(STDIN));
    if(empty($ext)) $ext = 'php';
    fwrite(STDOUT,"Please Input Find Content:");
    $search = trim(fgets(STDIN));
    fwrite(STDOUT,"Please Input Replace Content:");
    $replace = trim(fgets(STDIN));
    echo "正在目录'".$dir."'查找后缀为'".$ext."'的文件,将内容'".$search."'替换为'".$replace."',请稍后... ";
    exec("find ".$dir." -name '*.".$ext."'  -exec sed --in-place 's/".$search."/$replace/g' {} ;");
    echo "Replace Completed! ";
}
?>

您可能感兴趣的文章:
inux shell初级入门教程
sed命令例解
PHP与命令行交互(实现查找和替换)
python写完程序怎么运行
shell脚本命令行参数用法简介
PHP 常用命令行
shell 去掉每行结尾的空格,附sed知识
shell 中 &&和
关于CGI 和 PHP-FPM的对比分析
JQuery与iframe交互实现代码

[关闭]
~ ~