Search and Replace in Several Files Using PHP and Perl

October 27, 2007 § 4 Comments

Search and replace of keywords are very important in programming especially when you are working with files. I prepared and released a PHP class for this purpose. You can check it here:

You can use this class very easily. It has also an option to log changes/replacements that have been done in the most recent operation. I want to show you how easily we can do the search and replace stuffs using this class:

————- Code ——————

$path = "/path/to/your/dir"; //setting search path
$logFile = "/path/to/your/logFile"; //setting log file

$obj = new TextSearch();

//setting extensions to search files within

$obj->addExtension('php');//adding an extension to search within


//setting replacement text if you want to replace matches with that

$obj->startSearching($path);//starting search

$obj->showLog();//showing log

$obj->writeLogToFile($logFile); //writting result to log file


------------ Code -------------------

Now that we have the PHP class to do the search and replace within files. I want to show you a Perl code snippet that I got recently while searching net and I then thought I would share that. Here it is:

——————– Code ————

#!/usr/bin/perl -w ## More scripts and tips can be found at



# Search and replace in several files


# I throw this file in my ~/bin/
# Edit the variables $search, $replace and perhaps you want to change
# the globbing then I jump to the directory with the files and just
# execute (make sure that ~/bin is in your $PATH)
use strict;

my @infiles = glob("*.html");

my $search ='dilbert';

my $replace ='wally';

# Here we go.........

foreach my $file (@infiles){

print "Processing $file\n";

open(FH,$file) || die "Cannot load $file";

my @lines=<FH>;


my $match=0;

foreach my $line (@lines){

if($line =~ s/$search/$replace/g){





print "...Saving $file\n";

open(FS,">$file") || die "Cannot save $file";

print FS @lines;




——————– Code ————

I found this code in

Hope these two codes will come to your helps.




A Short Guideline to Create Password Protected Web Directory Using .htpasswd

October 24, 2007 § 2 Comments

Because of some consecutive requests on “how to create password protected web directory”, I am putting a short guideline so I can refer them to this page in future:

  1. Make an htaccess file (in your desired directory) like this:


AuthUserFile /your/path/passwordprotected/path/.htpasswd

AuthName “Display Text”

AuthType Basic

Require valid-user


  1. Make the .htpasswd file in the specified directory.

3. Add username and password (to enter this web directory)

You can add and update user and password by using:

>htpasswd –bm /your/path/passwordprotected/path/.htpasswd username password

b: Batch mode which facilitates you to enter user and password in the command prompt.

m: Use MD5 encryption for passwords. You can use “d” for crypt() generated passwords, “s” for SHA1 passwords, “p” for plain text passwords.

Once these steps are done, the .htpasswd looks like:





[I have created user “rupom” with password “rupom” and user “phpResource” with password “phpResource”].

Now, go to the URL where this is done, you will find what you expected.

Done! Enjoy !!

See for manual of htpasswd.



Hello AOL Address Book Grabber Seekers

October 21, 2007 § 5 Comments

For AOL address book grabber seekers:

I am getting a lot of requests of “AOL Address Book Grabber” PHP code. For your information – please don’t send me emails. Instead, please fill the following form:

I am a very busy guy and don’t have time to reply to all emails separately. So I thought I would collect interested emails using this form and then reply to the emails on each Saturday. I understand your need and importance of time, but I am sorry I can’t manage time except Saturday.

You may ask why I am not putting a download option of the code. I would like to reply I am sorry I have problems.

Best regards.


Where Am I?

You are currently viewing the archives for October, 2007 at Rupom Here.