Welcome to WebmasterWorld Guest from 54.205.74.11

Forum Moderators: brotherhood of lan & mack

replacing a word with a link on whole site

   
6:30 pm on Feb 28, 2003 (gmt 0)

10+ Year Member



i have site with about 550 pages, and i want to put a link on one specific name which is nearly on every page. how do i do this? i can't make make it with notetab because it also replaces that name in the meta tags or in the filenames - and i just want to put a link on that name when it occurs in the body part of a page. is there a trick for this?
6:35 pm on Feb 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am not seeing anything in note tab that will let you refine the search to just the body. Does this word appear next to other words consistently in the body?
6:37 pm on Feb 28, 2003 (gmt 0)

WebmasterWorld Senior Member rcjordan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Try adding the leading and trailing spaces in your find and replace, ala:

find: " some name "
replace: " <a href>some name</a> "

Depending on your meta construction, you might have trouble there, but the filenames shouldn't be disturbed.

6:42 pm on Feb 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I believe the only safe approach would be to use a HTML::Parser [perldoc.com] to parse the files and then replace the word only in ordinary text and not in any tags.

Andreas

6:43 pm on Feb 28, 2003 (gmt 0)

10+ Year Member



>Does this word appear next to other words consistently in the body?

no, that's the problem, it appears everywhere in combination with different words.

is there a programm which can do it only for the body? can dreamweaver do it?

7:06 pm on Feb 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Dreamweaver can do it but proceed with a little caution. Try a test first on the page which you have open in Dreamweaver and instead of doing just the word or changes you require either choose a complete sentence or the contents of a table including the table tags, this way you are keeping the table the same but only changing the content from either text or text and hyperlinks combined.

<table width="70%" border="0" cellspacing="0" cellpadding="4"><tr>
<td>This is the text I want to change</td>
</tr></table>

Do a find and replace and replace whatever you want changing in the table

<table width="70%" border="0" cellspacing="0" cellpadding="4"><tr>
<td>This is the link I want to add <a href="http://www.mysite.com">HOME</a></td>
</tr></table>

Dreamweaver wil warn you that any changes can't be undone with any pages which are not open

You can also do a find and replace for the head content for any changes you may require

Have Fun

7:18 pm on Feb 28, 2003 (gmt 0)

10+ Year Member



ncw164x, i don't understand. the name i want to replace with a link appears on the 500 pages in very diverse variations, there is nearly no single sentence or single tab which appears a second or third time somewhere else. the only thing which helps, is:

1) i want ro replace ALL words on ALL pages
2) i want ro replace all words in the BODY which are visible to the user

so that the word when it appears in the meta tag or in a filename remains untouched, you know what i mean?

how can dreamweaver do this? with the normal "find and replace"-function it can't work because it can't exclude the non-body-part

7:23 pm on Feb 28, 2003 (gmt 0)

10+ Year Member



Dreamweaver.

If it doesn' work just press control Z, before saving the files.

7:32 pm on Feb 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This script will recursively look for html files and replace a certain pattern with some string.


#!/usr/bin/perl -w
#
use strict [perldoc.com];
#
use File::Find [perldoc.com];
use HTML::Parser [perldoc.com];
#
my $r = 0;
my $p = HTML::Parser->new(api_version => 3);
$p->handler( start => sub {
$r=1 if shift eq 'body';
$r=0 if shift eq 'a';
print OUT pop; }, "tagname,tagname,text");
$p->handler( end => sub {
$r=0 if shift eq 'body';
$r=1 if shift eq 'a';
print OUT pop; }, "tagname,tagname,text");
$p->handler( text => sub {
print(OUT shift), return unless $r;
local $_ = shift;
s!$ARGV[1]!$ARGV[2]!go;
print OUT }, "text");
$p->handler( default => sub { print OUT shift }, "text");
#
find(sub [perldoc.com] {
return [perldoc.com] if -d $File::Find::name;
return [perldoc.com] unless $File::Find::name =~ /\.html?$/;
open [perldoc.com] 'OUT', ">$File::Find::name.temp"
or die "Can't open $File::Find::name.temp: $!\n";
$r = 0;
$p->parse_file($File::Find::name)
or die "Can't parse file: $File::Find::name.temp: $!\n";
close [perldoc.com] 'OUT';
rename [perldoc.com]($File::Find::name, "$File::Find::name.orig");
rename("$File::Find::name.temp", $File::Find::name);
}, $ARGV[0] ¦¦ die);

Call it like so:


[*nix]./htmlrep /path/to/html/files name link
[windows]perl htmlrep c:\path\to\html\files name link

A real life example would be


Perl [perl.com] htmlrep c:\path\to\html\files "Aaron C." "<a href=''>Aaron C.</a>"

As you see this works both on GNU/Linux and Windows. If you donīt have it just grab the ActiveState Perl [perl.com] package and install it (Microsoft Installer - just one click). The script will then run without any configuration.

HTH Andreas


Note: The WebmasterWorld posting software deletes spaces preceding the exclamation point "!" character. It also replaces a solid vertical pipe symbol with a broken vertical pipe "¦" symbol. Both of these changes will need to be undone in any code you copy from WebmasterWorld. Make sure to include a space preceding the "!" in mod_rewrite code, and always replace "¦" with a solid vertical pipe.
7:34 pm on Feb 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



BTW this is smart enough not to turn the link into a link if the name is already a link ;). Does that make sense? It should.

Andreas

8:05 pm on Feb 28, 2003 (gmt 0)

10+ Year Member



thank you very much andreasfriedrich! i have never seen such a thing before- i think it's time for studying it :-)
9:18 pm on Feb 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



HuhuFruFru
Yes your right what your changing is only in the body so that the word when it appears in the meta tag or in a filename remains untouched.
I had to change 15,000 pages because of a word which was trade marked, in meta tags and in also the body of the page. it was used as text and also as part of the hyperlink, it was used in any variation you could think of and many times on each page in different formats. This is different to what your trying to achieve

andreasfriedrich script seems to be the best option and I will study this script as well

 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month