PHP Batch replacement program's specific implementation code _php instance

Source: Internet
Author: User

The code is as follows:

Copy Code code as follows:



<?php


/***************************************************************************


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 horse in bulk and to update some contents of the page in bulk.


This procedure is suitable for modifying UTF-8 pages.

/***************************************************************************
*
* This are 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 to perform 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 of the/remove
if (substr ($dir, -1) = = '/') $dir = substr ($dir, 0,strrpos ($dir, "/"));

List all Catalogs
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 "<p> $v </p>"; Hx_dirtree ($file);
}else{
$ext =substr (STRRCHR ($v, "."), 1);
if (In_array ($ext, $arrext)) {
echo "<li> $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 ' </li> ';
}
}
}
$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 "<li> $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 ' </li> ';
}
}
}
Closedir ($DH);
}

}


Write out a function of the process of generating a file


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;


}


? &gt;&lt;! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "&gt;


&lt;html xmlns= "http://www.w3.org/1999/xhtml" &gt;


&lt;head&gt;


&lt;meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "&gt;


&lt;title&gt; Batch Replacement Program | Trojan Batch Delete _www.itlearner.com&lt;/title&gt;


&lt;style type= "Text/css" &gt;


Body{background: #FFFFFF; color: #000; font-size:12px;}


#top {text-align:center;}


h1,p,form{margin:0;padding:0;}


h1{font-size;14px;}


&lt;/style&gt;


&lt;/head&gt;


&lt;body&gt;


&lt;div id= "Top" &gt;


&lt;h1&gt; Batch Replacement Program (UTF-8 edition) &lt;/h1&gt;


&lt;div&gt; This program can scan all files in the specified directory for &lt;strong&gt; content replacement &lt;/strong&gt;. It can be used to delete the horse in bulk and to update some contents of the page in bulk. &lt;br/&gt;


In the case of a very large number of files, this operation is a comparison of server resources, make sure that the script timeout limit time allows changes, or you may not be able to complete the operation. &lt;/div&gt;


&lt;/div&gt;




&lt;form action= "&lt;?=$_server[' Script_name ']?&gt;" Name= "Form1" target= "Stafrm" method= "POST" &gt;


&lt;table width= "border=" 0 "align=" center "cellpadding=" 3 "cellspacing=" 1 "bgcolor=" #666666 "&gt;


&lt;tr&gt;


&LT;TD width= "10%" bgcolor= "#FFFFFF" &gt;&lt;strong&gt; start root path:&lt;/strong&gt;&lt;/td&gt;


&LT;TD width= "90%" bgcolor= "&gt;&lt;input name=" SearchPath "type=" text "id=" SearchPath "value="./test "Size=" "/&gt;


The point represents the current directory, the end does not add/&lt;input type= "checkbox" Name= "Isall" value= "1"/&gt; contains all directories under this directory &lt;/td&gt;


&lt;/tr&gt;


&lt;tr&gt;


&LT;TD bgcolor= "#FFFFFF" &gt;&lt;strong&gt; file name extension:&lt;/strong&gt;&lt;/td&gt;


&LT;TD bgcolor= "#FFFFFF" &gt;&lt;input name= "ShortName" type= "text" id= "ShortName" "size=" value= "php|htm"/&gt;


Please use | separate &lt;/td&gt;


&lt;/tr&gt;


&lt;tr id= "rpct" &gt;


&LT;TD height= "colspan=" 2 "bgcolor=" #FFFFFF "&gt;&lt;table width=" 100% "border=" 0 "cellspacing=" 1 "cellpadding=" 1 " &gt;


&lt;tr bgcolor= "#EDFCE2" &gt;


&LT;TD colspan= "4" &gt;&lt;strong&gt; content replacement options:&lt;/strong&gt; &lt;input type= "checkbox" Name= "Isreg" value= "1"/&gt; Using regular Expressions &lt;/td&gt;


&lt;/tr&gt;


&lt;tr&gt;


&LT;TD colspan= "4" &gt; Replace content Classes use string substitution by default, or you can use regular expressions (tick-check). "Replace with" if not filled out, means to delete "replacement content." &lt;/td&gt;


&lt;/tr&gt;


&lt;tr&gt;


&LT;TD width= "10%" &gt; Replace content:&lt;/td&gt;


&LT;TD width= "36%" &gt;&lt;textarea name= "Sstr" id= "Sstr" style= "width:90%;height:45px" &gt;&lt;/textarea&gt;&lt;/td &gt;


&LT;TD width= "10%" &gt; Replaced by:&lt;/td&gt;


&lt;td&gt;&lt;textarea name= "Rpstr" id= "Rpstr" style= "width:90%;height:45px" &gt;&lt;/textarea&gt;&lt;/td&gt;


&lt;/tr&gt;


&lt;/table&gt;&lt;/td&gt;


&lt;/tr&gt;


&lt;tr&gt;


&LT;TD colspan= "2" height= "align=" center "bgcolor=" #E2F5BC "&gt;&lt;input type=" Submit "name=" Submit "value=" Start the operation "class=" Inputbut "/&gt;&lt;/td&gt;


&lt;/tr&gt;


&lt;/table&gt;


&lt;/form&gt;


&lt;table width= "border=" 0 "align=" center "cellpadding=" 3 "cellspacing=" 1 "bgcolor=" #666666 "&gt;


&lt;tr bgcolor= "#FFFFFF" &gt;


&LT;TD id= "MTD" &gt;


&lt;div id= ' MDV ' style= ' width:100%;height:100; &gt;


&lt;iframe name= "stafrm" frameborder= "0" id= "stafrm" width= "100%" height= "100%" &gt;&lt;/iframe&gt;


&lt;/div&gt;


&lt;script type= "Text/javascript" &gt;


Document.all.mdv.style.pixelHeight = screen.height-450;


&lt;/script&gt; &lt;/td&gt;


&lt;/tr&gt;


&lt;/table&gt;

</body>

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.