PHP: Changing text in hundreds of files

Been a while since I wrote a tech entry. And this will be my first one,
involving PHP coding. [X)]

At work, I’m given the responsibility to manage a group of websites. The
codes are given to us by our counterparts, and we are running the sites under a
different domain name. There’s about 15 of them, each filled with 20-30 pages
and most have a few hundred lines of code. My task is to edit some links and
upload them to our webhosting server.

I soon got bored updating each single with the search feature in CrimsonEditor. So I took
this opportunity to write a script to automatically search and
destroy replace each occuring text with another.

To begin with, I planned and came up with the following pseudocode.

Define text to search;
Define text to replace;

Loop
through the directories and their sub-directories
  if file extension is
of htm, html, phtml
    read file
contents;
    search and replace;
   
save file and close;
  end if
end loop

I got a directory looping code from the online manual at PHP, submitted by
another person. The text to search and replace are submitted through a form, and
security won’t be important since I’m running it on my own development machine,
not on the public webhost.

This is the form that contains the array of search and replace text. To have
the values automagically created as an array to access, you add square brackets
at the end of the form input names.

<form method=”post”
action=”updater.php”>
 <table border=”0″ cellspacing=”0″
cellpadding=”2″ width=”900″>
  <tr><th>Replace
this</th><th>With
this</th></tr>
  <tr>
   <td><input
type=”text” name=”search[]”
value=”www.hotels-direct-paris.com”></td>
   <td><input
type=”text” name=”replace[]”
value=”paris-hotels.bestavailablerates.com”></td>
  </tr>
  <tr>
   <td><input
type=”text” name=”search[]”
value=”www.hotels-direct-venice.com”></td>
   <td><input
type=”text” name=”replace[]”
value=”venice-hotels.bestavailablerates.com”></td>
  </tr>
  <tr>
   <td><input
type=”text” name=”search[]”
value=”www.hotels-direct-spain.com”></td>
   <td><input
type=”text” name=”replace[]”
value=”spain-hotels.bestavailablerates.com”></td>
  </tr>
 </table>
 <div
align=”center”><input type=”submit” name=”updaterSubmit”
value=”Update”></div>
</form>

Here’s the PHP Code that gets triggered when the form is submitted. It calls
the update function and runs the code on the directory I hardcoded. Always make
backup copies! [:)]

<?php
 if (isset($_POST[‘updaterSubmit’]) &&
$_POST[‘updaterSubmit’]==’Update’)
{
  update(‘C:\Inetpub\wwwroot\BARwebsites\testcopies\\’);
 }
?>

And the update() recursive-function that loops through the directories,
replacing text

function update($curpath) {
 $dir =
dir($curpath);
 echo
“<b>$curpath</b><blockquote>”;
 while ($file =
$dir->read()) {
  if($file != ‘.’ && $file != ‘..’)
{
   if (is_dir($curpath.$file))
{
    update($curpath.$file.’\\’);
   }
else {
    if (strpos($file,’.htm’) ||
strpos($file,’.html’) || strpos($file,’.phtml’))
{
     echo $file.'<br />’; // display
filename
     $result =
str_replace($_POST[‘search’], $_POST[‘replace’],
file_get_contents($curpath.$file));
     $filepointer
= fopen($curpath.$file,’w+’); // opens
file
     fwrite($filepointer, $result); // truncate
file, write content
     fclose($filepointer); //
close
file
    }
   }
  }
 }
 $dir->close();
 echo
‘</blockquote>’;
 flush();
 return;
}

This line is where the interesting part comes. Click on the function names to
read up on them. Note that $_POST[‘search’] and $_POST[‘replace’] are globally
accessible, array variables.

$result = str_replace($_POST[‘search’],
$_POST[‘replace’], file_get_contents($curpath.$file));

Might look poorly coded to some, but I only want to accomplish my aim in a
short time without spending too much time on writing complex error-handling codes before they are passed in. I do welcome any positive/negative comments/advice to
help me improve my PHP skills. [:D]

Leave a Reply

Your email address will not be published. Required fields are marked *