PHP interacts with command line (implements find and replace)

Source: Internet
Author: User
Tags php tutorial stdin trim

<?php Tutorial
Ask for Input
Fwrite (STDOUT, "" Please select Option (Default are 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 to find directory (Default are 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." ";
EXEC ("Find" $dir. "-exec grep--exclude= ' *.svn/* '--'". $search. "' {} + ", $output);
foreach ($output as $val) {
echo "$val";
}
}else{//Write input back
Fwrite (STDOUT, "please Input to find directory (Default are 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 "is in Catalog". $dir. "' Find suffix ' ". $ext." ' file, the content ' ". $search." ' Replace with ' ". $replace." ', please later ... ";
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.