Welcome to WebmasterWorld Guest from 23.22.46.195

Forum Moderators: coopster & jatar k

Script to search and replace in multiple files

PERL or PHP

   
12:08 am on Jun 18, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have 1200 static pages where I want to replace the existing title tag with what is contained in the h1 tag on the same page.

Any ideas how to do this?

12:40 am on Jun 18, 2003 (gmt 0)

10+ Year Member



I dont understand what you want to do?

Are you looking for a program to do this or are you wanting to switch to a PHP/PERL option?

1:40 am on Jun 18, 2003 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



I believe toolman wants to take a directory with 1200 pages

open a pointer to a page
grab the text from the H1
write it into the TITLE
close the pointer

repeat as necessary

PERL or PHP don't matter, I imagine.

sounds like regex to me

1:50 am on Jun 18, 2003 (gmt 0)

10+ Year Member



ermm..
too difficult to do in 1 line perl..
but with more lines, yes.
I assume you're on a *nix machine.


@files = `find /your/path -name '*.html'`;
foreach $sing(@files) {
chomp $sing;
my $replace;
if(! -s($sing)) {
print "OOPS.. file [$sing] does not exist..\n";
next;
}
my $cont = `cat $sing`;
if($cont =~ /\<h1\>([^\<]+)\<\/h1\>/is) {
$replace = $1;
}
else {
print "OOPS.. in file [$sing] no h1 tag..\n";
next;
}
if(($cont =~ s/(\<title\>)[^\<]+(\<\/title\>)/$1$replace$2/is)<1){
print "OOPS.. in file [$sing] no title tag..\n";
next;
}
open(W, ">$sing");
print W $cont;
close W;
}
print "DONE\n";
exit 0;

-cminblues

[edited by: jatar_k at 5:03 am (utc) on June 18, 2003]
[edit reason] disabled smilies [/edit]

5:10 am on Jun 18, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



duh

[edited by: toolman at 5:12 am (utc) on June 18, 2003]

5:12 am on Jun 18, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks cmin.

It works fine. I appreciate it.

 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month