You are viewing a plain text version of this content. The canonical link for it is here.
Posted to embperl@perl.apache.org by Frank Sweetser <fs...@WPI.EDU> on 2002/03/18 17:13:31 UTC

transforming words in text into hyperlinks

I'm building a page that will have a bunch of nodes.  Each node has a block
of text.  What I'd like to be able to do is search within that block of
text for a given list of words, and for each word that's found, turn it into
a hyperlink, unless it's a) part of an html tag or b) already part of a
link.

Does anyone have any good sugestions or pointers on how I might pull this
one off?

-- 
Frank Sweetser fs at wpi.edu, fs at suave.net        | $ x 18
Full-time WPI Network Tech, Part time Linux/Perl guy |
"Don't confuse what works with what's right."
              - Kyle Jones, author of VM

---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org


Re: transforming words in text into hyperlinks

Posted by Frank Sweetser <fs...@WPI.EDU>.
On Mon, Mar 18, 2002 at 11:30:22AM -0500, Kee Hinckley wrote:
> Use EmbperlObject.  In the template build your search terms and
> then do something like the following:

Perfect - I can definatelly adapt this to what I'm doing.  Thanks!

-- 
Frank Sweetser fs at wpi.edu, fs at suave.net        | $ x 18
Full-time WPI Network Tech, Part time Linux/Perl guy |
At many levels, Perl is a "diagonal" language.
             -- Larry Wall in <19...@wall.org>

---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org


Re: transforming words in text into hyperlinks

Posted by Kee Hinckley <na...@somewhere.com>.
At 11:13 AM -0500 3/18/02, Frank Sweetser wrote:
>I'm building a page that will have a bunch of nodes.  Each node has a block
>of text.  What I'd like to be able to do is search within that block of
>text for a given list of words, and for each word that's found, turn it into
>a hyperlink, unless it's a) part of an html tag or b) already part of a
>link.

Well, I'm not sure if this is what you are looking for, but...

Use EmbperlObject.  In the template build your search terms and
then do something like the following:

        Execute({inputfile => '*', output => \$output, options => 512+16});
        $search = '';
        foreach $arg (split(/\s+/, $query)) {
            $search .= quotemeta($arg) . '|';
        }
        chop($search);
        # Match the search pattern as long as it isn't followed by a >, unless
        # we hit a < first.  That should keep it out of tags.
        $output =~ s,\b($search)(?![^<]*>),<font color="#FF0000">\1</font>,ig;
        my $oescmode = $escmode;
        $escmode = 0;
        print OUT "<b>Searching for " . $query . "</b><br>\n";
        print OUT $output;
        $escmode = $oescmode;

In this particular case, I'm doing something slightly different.  I want to flag the results of a search, but the concept is the same.

Here's an example of the output:

http://commons.somewhere.com/buzz/1997/PrivacyDistribution.html?Search=privacy+cookies


-- 

Kee Hinckley - Somewhere.Com, LLC
http://consulting.somewhere.com/
nazgul@somewhere.com

I'm not sure which upsets me more: that people are so unwilling to accept
responsibility for their own actions, or that they are so eager to regulate
everyone else's.

---------------------------------------------------------------------
To unsubscribe, e-mail: embperl-unsubscribe@perl.apache.org
For additional commands, e-mail: embperl-help@perl.apache.org