phpsearchreplaceincrement

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


I have 1,000 html pages. And a js script.

In the java code in the page I have somewhere "$ item_id = 1".

In every page I must change the id. I mean, in page_2.html I must have "$ item_id = 2" in the page_3.html I must have "$ item_id = 3" ... etc

I check more TextCrawlers softwares, but it offers me only search and replace, without incremental option.

Any ideas?


Solution

  • The Solution. First, replace in all your files all "$ item_id = 1", "$ item_id = 2" .. etc, with "$ item_id = wxyz" Then run the php script below in localhost, in the folder you keep the html files. You can name it 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;
    }
    
    ?>