Php batch replacement program implementation code

Source: Internet
Author: User

The Code is as follows:
Copy codeThe Code is as follows:
<? Php
/*************************************** ************************************
Batch-replace, v1.1
**************************************** ***********************************
File: batch-replace_utf8.php
Functionality: This program can scan all the files in the specified directory to replace the content. It can be used to delete Trojans in batches and to update some content on the page in batches.
This procedure applies to modifying the page of the UTF-8.

 

/*************************************** ************************************
*
* This program is free software; you can redistribute it and/or modify
* It under the terms of the GNU Lesser General Public License as published
* 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 execute 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']);
}


// Analyze shortname
$ Arrext = explode ("|", $ shortname );


If (! Is_dir ($ dir) return;
If ($ sstr = '') return;

// Remove the ending slash (/)
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 "<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 ){
$ Body2 = preg_replace ($ sstr, $ rpstr, $ body );
} Else {
$ Body2 = str_replace ($ sstr, $ rpstr, $ body );
}
If ($ body! = $ Body2 & $ body2! = ''){
Tofile ($ file, $ body2 );
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 ){
$ Body2 = preg_replace ($ sstr, $ rpstr, $ body );
} Else {
$ Body2 = str_replace ($ sstr, $ rpstr, $ body );
}
If ($ body! = $ Body2 & $ body2! = ''){
Tofile ($ dir. '/'. $ file, $ body2 );
Echo 'OK ';
} Else {
Echo 'no ';
}
Echo '</li> ';
}
}
}
Closedir ($ dh );
}

}
// Write the function of the file generation process
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 the pointer
Return $ file_name;
}
?> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> batch replacement program | batch deletion of Trojans _ www.itlearner.com </title>
<Style type = "text/css">
Body {background: # FFFFFF; color: #000; font-size: 12px ;}
# Top {text-align: center ;}
H1, p, form {margin: 0; padding: 0 ;}
H1 {font-size; 14px ;}
</Style>
</Head>
<Body>
<Div id = "top">
<H1> bulk Replacement Program (UTF-8 edition) <Div> This program can scan all files in the specified directory and <strong> replace content </strong>. It can be used to delete Trojans in batches and to update some content on the page in batches. <Br/>
When the number of files is very large, this operation occupies server resources. Make sure that the script time-out period can be changed. Otherwise, the operation may fail. </Div>
</Div>


<Form action = "<? = $ _ SERVER ['script _ name']?> "Name =" form1 "target =" stafrm "method =" post ">
<Table width = "95%" border = "0" align = "center" cellpadding = "3" cellspacing = "1" bgcolor = "#666666">
<Tr>
<Td width = "10%" bgcolor = "# FFFFFF"> <strong> Start root path: </strong> </td>
<Td width = "90%" bgcolor = "# FFFFFF"> <input name = "searchpath" type = "text" id = "searchpath" value = ". /test "size =" 20 "/>
Point indicates the current directory. Do not add/<input type = "checkbox" name = "isall" value = "1"/> include all directories in this directory. </td>
</Tr>
<Tr>
<Td bgcolor = "# FFFFFF"> <strong> File Extension: </strong> </td>
<Td bgcolor = "# FFFFFF"> <input name = "shortname" type = "text" id = "shortname" size = "20" value = "php | htm"/>
Separate multiple IDs with | </td>
</Tr>
<Tr id = "rpct">
<Td height = "64" colspan = "2" bgcolor = "# FFFFFF"> <table width = "100%" border = "0" cellspacing = "1" cellpadding = "1 ">
<Tr bgcolor = "# EDFCE2">
<Td colspan = "4"> <strong> content replacement options: </strong> <input type = "checkbox" name = "isreg" value = "1"/> use regular expressions </td>
</Tr>
<Tr>
<Td colspan = "4"> by default, the replacement content class uses string replacement, or you can use a regular expression (check required ). "Replace with" if not entered, it means to delete "replace content ". </Td>
</Tr>
<Tr>
<Td width = "10%"> Replace: </td>
<Td width = "36%"> <textarea name = "sstr" id = "sstr" style = "width: 90%; height: 45px"> </textarea> </td>
<Td width = "10%"> replace with: </td>
<Td> <textarea name = "rpstr" id = "rpstr" style = "width: 90%; height: 45px"> </textarea> </td>
</Tr>
</Table> </td>
</Tr>
<Tr>
<Td colspan = "2" height = "20" align = "center" bgcolor = "# E2F5BC"> <input type = "submit" name = "Submit" value = "Start execute the operation "class =" inputbut "/> </td>
</Tr>
</Table>
</Form>
<Table width = "95%" border = "0" align = "center" cellpadding = "3" cellspacing = "1" bgcolor = "#666666">
<Tr bgcolor = "# FFFFFF">
<Td id = "mtd">
<Div id = 'magrou' style = 'width: 100%; height: 100; '>
<Iframe name = "stafrm" frameborder = "0" id = "stafrm" width = "100%" height = "100%"> </iframe>
</Div>
<Script type = "text/javascript">
Document. all. DV. style. pixelHeight = screen. height-450;
</Script> </td>
</Tr>
</Table>

</Body>
</Html>

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.