php batch replacement procedures specific implementation code

  • 2020-10-23 20:03:26
  • OfStack

The code is as follows:


<?php
/***************************************************************************
batch-replace, v1.1
***************************************************************************
file: batch-replace_utf8.php
functionality:  This procedure can scan the specified directory of all files, content replacement. Can be used to be batch horse deletion and batch update page some content. 
 This procedure applies to pairs UTF-8 The page is modified. 
 
/***************************************************************************
*
* 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 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 of the shortname
$arrext = explode ("|",$shortname);

if (!is_dir($dir)) return;
if ($sstr == '') return;
// At the end of the / To get rid of 
if(substr($dir,-1)=='/') $dir = substr($dir,0,strrpos($dir,"/"));
// List all the 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 out the process of generating the 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 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 | Trojans mass deletion _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> Batch replacement program (UTF-8 version )</h1>
<div> This procedure can scan the specified directory of all files <strong> Content to replace </strong> . Can be used to be batch horse deletion and batch update page some content. <br/>
 In the case of a very large number of files, this operation is taking up server resources. Make sure that the script timeout limit allows changes, or the operation may not complete. </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>&nbsp; Initial root path: </strong></td>
<td width="90%" bgcolor="#FFFFFF"><input name="searchpath" type="text" id="searchpath" value="./test" size="20" />
 Click to indicate the current directory, do not add the end / <input type="checkbox" name="isall" value="1" /> Contains all directories under this directory </td>
</tr>
<tr>
<td bgcolor="#FFFFFF"><strong>&nbsp; File extension: </strong></td>
<td bgcolor="#FFFFFF"><input name="shortname" type="text" id="shortname" size="20" value="php|htm" />
 Please use multiple | separated </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"> Replacement content classes use string substitution by default, or regular expressions can be used ( Need to check the ) . " Replace with " If you don't fill it in, you delete it " Alternative content " . </td>
</tr>
<tr>
<td width="10%">&nbsp; Replacement content: </td>
<td width="36%"><textarea name="sstr" id="sstr" style="width:90%;height:45px"></textarea></td>
<td width="10%"> On behalf of   in   To: </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 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='mdv' style='width:100%;height:100;'>
<iframe name="stafrm" frameborder="0" id="stafrm" width="100%" height="100%"></iframe>
</div>
<script type="text/javascript">
document.all.mdv.style.pixelHeight = screen.height - 450;
</script> </td>
</tr>
</table>
</body>
</html>


Related articles: