我有 1,000 个 html 页面。还有一个js脚本。
在页面的java代码中我有“$ item_id = 1”。
在每个页面中我都必须更改 id。我的意思是,在 page_2.html 我必须有 "$ item_id = 2" 在 page_3.html 我必须有 "$ item_id = 3" ... 等等
我检查了更多 TextCrawlers 软件,但它只为我提供搜索和替换,没有增量选项。
有什么想法吗?
解决方案。首先,将所有文件中的所有
"$ item_id = 1"
、"$ item_id = 2"
.. 等替换为 "$ item_id = wxyz"
然后在 localhost 中运行下面的 php 脚本,在您保存 html 文件的文件夹中。你可以给它命名replace_increment.php
<?php
$to_be_replaced = 'wxyz'; // exactly what it wants replaced
$nr_start = 1; // from which no start counting
$path_files = getcwd();
// echo $path_files;
$excluded_files = array(
'.htaccess',
'robots.txt',
'.ftpquota',
'search.html',
'replace_increment.php',
);
$file_list = get_list_dir($path_files, false, 'file', true, $excluded_files);
if ( is_array($file_list) && count($file_list)) {
// sort list files
natsort($file_list);
//echo '<pre>'.var_export($file_list,1).'</pre>';
foreach($file_list as $file) {
$original_content = file_get_contents($file);
// Search the file for the replacement piece
if ( stristr($original_content, $to_be_replaced) ) {
// if found, replaces
$content_modified = str_replace($to_be_replaced, $nr_start, $original_content);
// remove the blank lines
$content_modified = str_replace("\n\n\n\n", "\n\n", $content_modified);
// save the file contents back
$is_saved = file_put_contents($file, $content_modified);
if ( ! $is_saved ) {
die('Error: Unable to modify the file '.$file.'. I stayed at number '.$nr_start);
}
$nr_start++;
}
}
echo 'They were checked '.count($file_list).' files and the last number is '.($nr_start - 1);
} else {
echo 'Files Not found, check the file path';
}
function get_list_dir($path, $depth = false, $type = 'all', $inc = true, $exclude = array(), $max=95) {
// Set list
$list = array();
// directory element is determined depending on the operating system
$elm = ( stristr(PHP_OS, 'win') === false ) ? '/' : '\\';
if (empty($path))
return false;
if (!is_dir($path))
return false;
// memorizes the current path
$base_path = getcwd();
// change to the path specified
if ($base_path != $path) {
$is_changed = chdir($path);
if (!$is_changed)
return false;
}
$required_path = getcwd();
if (!$required_path)
return false;
// read path required
$director = opendir($required_path);
if (!$director) {
// return to the base path
chdir($base_path);
return false;
}
// reads the current directory
$read = readdir($director);
if ($read === false) {
// return to the base path
chdir($base_path);
return false;
}
while ($read) {
// excluding files / directories unwanted
if (!in_array($read, $exclude)) {
// check what type is required
switch ($type) {
default:
case 'all': // returns all files and directories found
// to memorize what is currently
$list[] = ( $inc ) ? $required_path . $elm . $read : $read;
// if is director and requires completion
if (is_dir($read) && $depth) {
if ( $max<1) {
$list[] = 'Too many subdirectories, indexing interrupted.';
break;
} else {
// browse the directory
$x = get_list_dir($read, $depth, $type, $inc, $exclude, $max-1);
$list = array_merge($list,$x);
}
}
break;
case 'dir': // only returns the list of directories found
// if is director
if (is_dir($read)) {
// to memorize what is currently
$list[] = ( $inc ) ? $required_path . $elm . $read : $read;
// if requires completion
if ($depth) {
if ( $max<1) {
$list[] = 'Too many subdirectories, indexing interrupted.';
break;
} else {
// browse the directory
$x = get_list_dir($read, $depth, $type, $inc, $exclude, $max-1);
$list = array_merge($list,$x);
}
}
}
break;
case 'file': // only returns the list of files found
// check if file
if (is_file($read)) {
// to memorize what is currently
$list[] = ( $inc ) ? $required_path . $elm . $read : $read;
}
// else if is folder and it requires completion
elseif ($depth) {
if ( $max<1) {
$list[] = 'Too many subdirectories, indexing interrupted.';
break;
} else {
// browse the directory
$x = get_list_dir($read, $depth, $type, $inc, $exclude, $max-1);
$list = array_merge($list,$x);
}
}
break;
} // end switch
} // end exclude
// go to next
$read = readdir($director);
} // end while
// director closes
closedir($director);
// returns to the initial path
chdir($base_path);
// return
return $list;
}
?>
您可以使用硬编码或动态的基本思想来制作一个小程序。 因此,您定义了多个页面(示例适用于相同名称)。
您采用 for 循环并像(在 C# 中)一样使用它:
using System.IO;
using System.Text.RegularExpressions;
for (int i = 1; i <= numberOfPages; i++)
{
File.WriteAllText("Path\\page_" + i + ".html", Regex.Replace(File.ReadAllText("Path\\page_" + i + ".html"), @"\$ item_id = 1", "$ item_id = " + i));
}
如果您有不同的文件名,则需要使用正则表达式来获取正确的编号,并使用 for-each 循环遍历包含文件名的列表。