homepage Welcome to WebmasterWorld Guest from 54.163.84.199
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Script to search and replace in multiple files
PERL or PHP
toolman




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

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?

 

Knowles




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

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?

jatar_k




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

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

cminblues




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

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]

toolman




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

duh

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

toolman




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

Thanks cmin.

It works fine. I appreciate it.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved