Advertisement
aishaon

Find & Replace

Jul 23rd, 2023
1,059
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.58 KB | Source Code | 0 0
  1. <!doctype html>
  2. <html>
  3. <head>
  4.   <meta charset="utf-8">
  5.   <meta name="robots" content="noindex, nofollow"/>
  6.   <title>Search and replace files</title>
  7. </head>
  8. <body style="margin: 0; padding: 0;">
  9. <?php
  10. $err_arr = array(0 => '', 1 => '');
  11. if (isset($_POST['submit'])) {
  12.   if ($_POST['text'] == '' || $_POST['dir'] == '') {
  13.     $err = ' style="border: 1px solid red"';
  14.     if ($_POST['text'] == '')
  15.       $err_arr[0] = $err;
  16.     if ($_POST['dir'] == '')
  17.       $err_arr[1] = $err;
  18.   } else {
  19.  
  20.     set_time_limit(0);
  21.     error_reporting(E_ALL);
  22.  
  23.     $dir = trim($_POST['dir']);
  24.     $text = stripslashes($_POST['text']);
  25.     $retext = stripslashes($_POST['retext']);
  26.     $replace = isset($_POST['replace']) ? $_POST['replace'] : 0;
  27.     $ext = explode(',', $_POST['ext']);
  28.     $cnt = 0;
  29.  
  30.     function scan_dir($dirname)
  31.     {
  32.       global $text, $retext, $replace, $ext, $cnt;
  33.  
  34.       $dir = opendir($dirname);
  35.  
  36.       while (($file = readdir($dir)) !== false) {
  37.         if ($file != "." && $file != "..") {
  38.           $file_name = $dirname . "/" . $file;
  39.           if (is_file($file_name)) {
  40.             $ext_name = substr(strrchr($file_name, '.'), 1);
  41.             if (in_array($ext_name, $ext) || $file_name == $dirname . '/search_replace.php')
  42.               continue;
  43.             echo $file_name . '> ';
  44.             $content = file_get_contents($file_name);
  45.             if (strpos($content, $text) !== false) {
  46.               $cnt++;
  47.               if ($replace) {
  48.                 $content = str_replace($text, $retext, $content);
  49.                 file_put_contents($file_name, $content);
  50.               }
  51.  
  52.               echo '<b>' . $cnt . '</b>: ' . $file_name . '<br>';
  53.             }
  54.           }
  55.  
  56.           if (is_dir($file_name)) {
  57.             scan_dir($file_name);
  58.           }
  59.         }
  60.  
  61.       }
  62.  
  63.       closedir($dir);
  64.     }
  65.  
  66.     $start_time = microtime(true);
  67.  
  68.     echo '<div style="padding: 10px; width: 98%; background: #FFEAEA; border: 2px solid #FFB0B0; margin-bottom: 20px">';
  69.  
  70.     if ($replace)
  71.       echo '<h2>Text replace:</h2>';
  72.     else
  73.       echo '<h2>Text search:</h2> ';
  74.  
  75.     scan_dir($dir);
  76.  
  77.     if (!$cnt)
  78.       echo 'Not found';
  79.  
  80.     $exec_time = microtime(true) - $start_time;
  81.  
  82.     printf("<br /><b>Time: %f seg.</b></div>", $exec_time);
  83.   }
  84. }
  85. ?>
  86. <div style="padding: 10px; width: 100%; background: #E7F0F5; border: 2px solid #C5E7F6; text-align: left;">
  87.   <form method="post">
  88.     <table cellpadding="5" cellspacing="0" border="0" align="left">
  89.       <tr>
  90.         <td align="right">
  91.           <p align="left">
  92.             <strong>Text Search*:
  93.             </strong>
  94.           </p>
  95.           <p>
  96.             <textarea <?php echo $err_arr[0]; ?> name="text" cols="55" rows="8"><?php echo isset($text) ? $text : ''; ?></textarea>
  97.           </p>
  98.         </td>
  99.       </tr>
  100.       <tr>
  101.         <td align="right">
  102.           <p align="left">
  103.             <strong>Text Replace:
  104.             </strong>
  105.           </p>
  106.           <p>
  107.             <textarea name="retext" cols="55" rows="8"><?php echo isset($retext) ? $retext : ''; ?></textarea>
  108.           </p>
  109.         </td>
  110.       </tr>
  111.       <tr>
  112.         <td align="right">
  113.           <div align="left">
  114.             <strong>Replace:
  115.             </strong>
  116.             <input type="checkbox" <?php echo isset($replace) && $replace == 1 ? ' checked' : ''; ?> name="replace" value="1"/>
  117.           </div>
  118.         </td>
  119.       </tr>
  120.       <tr>
  121.         <td align="right">
  122.           <div align="left">
  123.             <strong>Not search this type files:
  124.             </strong>
  125.             <input type="text" size="33" name="ext"
  126.                    value="<?php echo isset($_POST['ext']) ? $_POST['ext'] :
  127.                        'gif,jpg,jpeg,png,zip,rar,pdf,css,flv,mp3,JPG,js,html,htm,xml,swf,doc,mpg,sxw,cdr,ini,txt,sql,xls,shtml,tmp'; ?>"/>
  128.           </div>
  129.         </td>
  130.       </tr>
  131.       <tr>
  132.         <td align="right">
  133.           <div align="left">
  134.             <strong>Folder:
  135.             </strong>&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;
  136.             <input
  137.                 <?php echo $err_arr[1]; ?> type="text" size="33" name="dir"
  138.                                            value="<?php echo isset($dir) ? $dir : '.'; ?>"
  139.                                            title='Enter ".", '/>
  140.           </div>
  141.         </td>
  142.       </tr>
  143.       <tr>
  144.         <td colspan="2" align="center">
  145.           <br/>
  146.           <input type="submit" name="submit" value="Search \ Replace"/>
  147.         </td>
  148.       </tr>
  149.     </table>
  150.   </form>
  151. </div>
  152. </body>
  153. </html>
Tags: php
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement