如何在 html 文件中搜索、替换 + 增量?

How can I search , replace + increment in a html file?

我有 1,000 html 页。和一个js脚本。

在页面的 java 代码中,我在某处有“$ item_id = 1”。

我必须在每个页面中更改 id。我的意思是,在 page_2.html 我必须 "$ item_id = 2"page_3.html 我必须 "$ item_id = 3" ... 等等

我查了更多的TextCrawlers软件,但它只提供搜索和替换,没有增量选项。

有什么想法吗?

听起来像是用您选择的脚本语言编写小脚本的工作:

  • 检索目录中 HTML 个文件的文件名
  • 对于每个文件名:
    • 解析文件名以将 id 提取到某个变量中 id,例如通过 /page_(\d+)\.html/ (example)
    • 读取文件
    • 搜索您的字词 "$ item_id = 1" 例如通过 /$ item_id = (\d+)/ (example)
    • 用变量 id
    • 中的字符串替换匹配项
    • 写文件

你可以用硬编码或动态的基本思想制作一个小程序。 所以你定义了一些页面(同名的例子)。

你采用 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 循环遍历包含文件名的列表。

解决方案。首先,将所有文件 "$ item_id = 1""$ item_id = 2" .. 等替换为 "$ item_id = wxyz" 然后 运行 下面本地主机中的 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;
}

?>