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:

http://www.phpclasses.org/browse/package/3211.html

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 ——————

<?php
require_once('TextSearch.class.php');
$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->setExtensions(array('html','txt')); 

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

$obj->setSearchKey('PHP');

//setting replacement text if you want to replace matches with that
$obj->setReplacementKey('phpResource');

$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

# http://www.edlin.org/

#

# 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 msr.pl (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>;

close(FH);

my $match=0;

foreach my $line (@lines){

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

$match=1;

}

}

if($match){

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

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

print FS @lines;

close(FS);

}

}

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

I found this code in http://www.edlin.org/perl/.

Hope these two codes will come to your helps.

Thanks.


Rupom

Advertisements

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:

———-.htaccess—————————————

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:

——————–.htpasswd————————————-

phpResource:$apr1$roLEO/..$ZXhi6TbX6L22wG78yhWtR/

rupom:$apr1$dcIUu…$8zByhyvWMi9oKHBReQv/n0

——————————————————————–

[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 http://httpd.apache.org/docs/1.3/programs/htpasswd.html for manual of htpasswd.

 

BEST REGARDS.

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:

http://www.rupom.com/aol/post-your-email.php

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.

Rupom

Where Am I?

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