📄 08c03-1.php
字号:
<?php// Read in all passed parameters and ensure they are correct:if (!(isset($argc))) { // Someone tried to run this through the web. echo "This script is for command line use only!"; exit();}// Now check that we have at least 2, and at most 3, parameters:// Remember that the filename appears as $argv[0] and counts as one.if (($argc < 3) || ($argc > 4)) { // Improper usage, give an error and exit: echo "Improper Usage! Please use the following: {$argv[0]} <regex_to_match> <replacement> [directory]"; exit();}// Now, if we've made it here, we have a valid number of parameters,// so store the data into appropriately named variables:$old = $argv[1];$new = $argv[2];// Grab the directory name, default to the current directory if none given$dir = isset($argv[3]) ? $argv[3] : '.';// Ensure at this point that the directory exists, otherwise exitif (!(is_dir($dir))) { echo "Error: Directory {$dir} does not exist!\n"; exit();}// Before we get any farther, if the directory string ends in a // '/' or '\' then remove it for later use:$dir_noslash = preg_replace('|([/\\\])$|', '', $dir);// Now we can begin our filename transformations. Open the directory:$d = dir($dir);// Loop through all the files:while (false !== ($file = $d->read())) { // Skip . and .., we don't want to try to rename them: if (($file == '.') || ($file == '..')) { continue; } // Perform the requested translation of the filename: $newname = preg_replace($old, $new, $file); // Guard against someone passing in regex that breaks: if (!(isset($newname))) { echo "Error: There has been a problem with the regex provided\n"; exit(); } // Now if this name is now different, then rename the file: if ($newname != $file) { rename("{$dir_noslash}/{$file}", "{$dir_noslash}/{$newname}"); }}?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -