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]

Gaming Goodness

Nice, I spent the past 3 days on intensive gaming and watched lots of anime. I can better cope with Doom3 now. I suspect it’s the frame rates. When I reduced the quality and adjusted some settings for better performance, I could actually play the game for 6 hours straight and only feel a little uncomfortable.


Doom3 is no longer scary. I’m now able to dodge fireballs and go up close to the fire imps and finish them off with a click on my shotgun~ [XD] I only jump at some parts where the demons jump out suddenly, giving me a fright. I try to relax myself but my eyes keep opening wide while playing. The best thing I love about this game is it’s mixing satanic elements with futuristic technology.

Finally, another task accomplished

I took a day off today to rest. Don’t feel well enough to work.


So today, I spent it finishing up the conversion of my blog to PHP, played a bit of Doom3.


Talking about Doom3, even though I had smooth framerates, I felt nauseous 10 minutes into the game. This happened to other games like HalfLife and Return to Castle Wolfenstein. And this sucks. [:(] I can’t enjoy FPS (first person shooter) games anymore. However, there’s an exception. So far I can cope well with CounterStrike. I don’t know the explanation for this…

Doom3

Things are going a lot busier now with Doom3 installed on my machine. It requires a rather mean machine to run it at high quality settings and fortunately, my machine meets those requirements. [XD] . There are people who manage to run it on GeForce 2, getting 15-50FPS but at very low quality settings. So low it’s doing injustice to the game.


I got surprised and jumped at 2 scenes, when I open the door and the fire imp jumps right towards me. And another when I approached a control panel with a zombie soldier lying on it. That fuck woke up and started shooting at me.


Apart from the scary gameplay, Doom3 has it’s own revolutionary 3D engine that’s going to be the new standard in FPS gaming. The realistic lighting and impressive physics are the reason why there’s huge commotion in forums when the game got officially released, and it’s bittorrent having hundreds of seeders and went from hundreds of leechers to twenty thousands in just an hour.


Gahh… Right now I have a bit of fever coming up and a running nose. It’s just one of those usual sickness. If it gets worse, I might go see a doctor and get MC~