PHP and command line interaction (find and replace)-PHP source code

Source: Internet
Author: User
Ec (2); & lt ;? Php tutorial AskforInputfwrite (STDOUT, & quot; PleaseSelectOption (DefaultisFind) [1] Find [2] ReplacePleaseInputNumber: & quot;); GetInput $ todotrim (fgets (STDIN )); if (empty script ec (2); script

// 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 (); // Current Directory
} 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 (); // Current Directory
} 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 "directory in progress '". $ dir. "'search suffix is '". $ ext. "'file, content '". $ search. "'replace '". $ replace. "', please wait... ";
Exec ("find ". $ dir. "-name '*. ". $ ext. "'-exec sed -- in-place's /". $ search. "/$ replace/G '{};");
Echo "Replace Completed! ";
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.