$text =~ s/widget/<a href='search.cgi?q=widget'>widget<\/a>/gi;
g = do it globally ... not just once
i = case insensitive.
From experience I would recommend using some kind of code around widget - there maybe times when you would want widget to appear in the text...
eg. (using # as the delimiter so you don't have to escape slashes).
$text =~ s#\[url\]widget\[/url\]#<a href='search.cgi?q=widget'>widget</a>#gi;
and better still:
$text =~ s#\[url\](.*?)\[/url\]#<a href='search.cgi?q=$1'>$1</a>#gi;
which would allow you to do this replace on
foo bar [url]widget/[/url/] bar non link widget foo [url]wodgits/[/url/] Note: url is the code used by brett... had to put in lots of nasty slashes - I think you see what I mean...
HTH
my $rules = join [perldoc.com] '', map [perldoc.com] {
join [perldoc.com]('', '$x =~ s!', $_, '!<a href="search.cgi?q=', $_, '">', $_', '</a>!gi')
} @words;
my $m = eval [perldoc.com] "sub [perldoc.com] {my \$x = shift [perldoc.com]; $rules; return [perldoc.com] \$x;}";
$string = &$m;
BTW how do you make sure that a term will be replaced only when it is ordinary text? If you had string like <p class="term">Some text about something</p> and applied the above code to replace term with a link you would end up with invalid code.
The HTML replace script [webmasterworld.com] I posted in Bag-O-Tricks for Perl - Letīs collect some nice and useful perl scripts [webmasterworld.com] will take care of that.
Andreas