PHP Bulk Replacement Program Instance Code _php tutorial

Source: Internet
Author: User
The code is as follows:

/***************************************************************************
Batch-replace, v1.1
***************************************************************************
file:batch-replace_utf8.php
Functionality: This program can scan all files in the specified directory for content substitution. It can be used to delete the bulk-mounted horse and to update some content of the page in bulk.
This program applies to the UTF-8 of the page to modify.

/***************************************************************************
*
* This program was free software; You can redistribute it and/or modify
* It under the terms of the GNU Lesser general public License as published by
* The free software Foundation; Either version 2 of the License, or
* (at your option) any later version.
*
***************************************************************************/

Set_time_limit (3600);


if ($_post[' Submit ']== ' start operation ') {
$dir = $_post[' SearchPath ');
$shortname = $_post[' shortname ');
$isall = $_post[' Isall ');
$isreg = $_post[' Isreg ');

if (!GET_MAGIC_QUOTES_GPC ()) {
$sstr = $_post[' sstr ');
$rpstr = $_post[' rpstr ');
} else {
$sstr = stripslashes ($_post[' sstr ');
$rpstr = stripslashes ($_post[' rpstr ');
}


Analysis ShortName
$arrext = Explode ("|", $shortname);


if (!is_dir ($dir)) return;
if ($sstr = = ") return;

Take the end/remove
if (substr ($dir, -1) = = '/') $dir = substr ($dir, 0,strrpos ($dir, "/"));

List all directories
if ($isall = = 1) {
Hx_dirtree ($dir);
}else{
Hx_dealdir ($dir);

}

Exit ();
}


function Hx_dirtree ($path = ".") {
Global $sstr, $rpstr, $isreg, $arrext;


$d = Dir ($path);
while (false!== ($v = $d->read ())) {
if ($v = = "." | | $v = = "..") continue;
$file = $d->path. " /". $v;
if (Is_dir ($file)) {
echo "

$v

"; Hx_dirtree ($file);
}else{
$ext =substr (STRRCHR ($v, "."), 1);
if (In_array ($ext, $arrext)) {
echo "
  • $file ";
    $body = file_get_contents ($file);
    if ($isreg = = 1) {
    $body 2 = preg_replace ($sstr, $rpstr, $body);
    }else{
    $body 2 = Str_replace ($sstr, $rpstr, $body);
    }
    if ($body! = $body 2 && $body 2! = ") {
    ToFile ($file, $body 2);
    Echo ' OK ';
    }else{
    Echo ' NO ';
    }
    Echo '
  • ';
    }
    }
    }
    $d->close ();
    }

    function Hx_dealdir ($dir) {
    Global $sstr, $rpstr, $isreg, $arrext;
    if ($dh = Opendir ($dir)) {
    while (false!== ($file = Readdir ($DH))) {
    if (filetype ($dir. '/'. $file) = = ' file ') {

    $ext =substr (STRRCHR ($file, "."), 1);
    if (In_array ($ext, $arrext)) {

    echo "

  • $file ";
    $body = file_get_contents ($dir. '/'. $file);
    if ($isreg = = 1) {
    $body 2 = preg_replace ($sstr, $rpstr, $body);
    }else{
    $body 2 = Str_replace ($sstr, $rpstr, $body);
    }
    if ($body! = $body 2 && $body 2! = ") {
    ToFile ($dir. '/'. $file, $body 2);
    Echo ' OK ';
    }else{
    Echo ' NO ';
    }
    Echo '
  • ';
    }
    }
    }
    Closedir ($DH);
    }

    }
    Write the process of generating a file out of function
    function ToFile ($file _name, $file _content) {
    if (Is_file ($file _name)) {
    @unlink ($file _name);
    }
    $handle = fopen ($file _name, "w");
    if (!is_writable ($file _name)) {
    return false;
    }
    if (!fwrite ($handle, $file _content)) {
    return false;
    }
    Fclose ($handle); Close pointer
    return $file _name;
    }
    ?>



    Bulk Replacement Program | Trojan Bulk Delete _www.itlearner.com




    Bulk replacement Program (version UTF-8)


    This program can scan all files in the specified directory for content Substitution。 It can be used to delete the bulk-mounted horse and to update some content of the page in bulk.

    In the case of a very large number of files, this operation compared to occupy server resources, make sure that the script time-out limit is allowed to change, or may not complete the operation.












    http://www.bkjia.com/PHPjc/371366.html www.bkjia.com true http://www.bkjia.com/PHPjc/371366.html techarticle the code is as follows: Php/*************************************************************************** batch-replace, v1.1 * * * *************************************************************...

  • 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.