powerhouse has asked for the wisdom of the Perl Monks concerning the following question:

I built a search feature on my wife's website, and I decided in the search function at the beginning, which it does not matter where because of how I did it, I had to add a variable in a hash ($in{highlight_text} = $_query;) Where $_query = param("q"); (Set just above this variable).

The hash (%in) is created at the beginning of the 'system' by cgi-lib (ReadParse(\%in)) and so I am just 'adding' a variable to it.

Now all the 'page content' is in a variable, let's call it $_page_content_var so at the end, just before I print it to the page, I am doing this:
my $__highlight_to_switch = qq~<font class="highlight">$in{highlight_t +ext}</font>~; $_page_content_var =~ s/$in{highlight_text}/$__highlight_to_switch/eg;
Now my problem is that since it's a 'search' function, I have a link with the query text in there something like this: <a href="http://www.mydomain.com/index.cgi?pg=search&q=$_query"> since $_query contains the text that I am searching for, it will inadvertantly 'switch' the q=$_query part of the text with the <font... text.

Is there a way to make this not do that? I don't want it to change stuff inside of html tags, just everything else on the page, so that it's highlighted for the end user to see the 'stuff' they searched for.


thx,
Richard

Replies are listed 'Best First'.
Re: Switching out text only in a variable
by Zaxo (Archbishop) on Nov 19, 2004 at 05:29 UTC

    How is you html search form written? The client should be providing the search term from user input, and it doesn't know anything about your $_query variable.

    The cgi-lib library is very old now. It won't cope with new-fangled standards like ';' between query terms. You'd do well to replace with CGI.pm, which has some cgi-lib compatibility functions if you don't want to do a lot of rewriting.

    Your substitution looks ok to me. You may want to split up the search text on spaces and do your substitution as an alternation of intividual search terms.

    After Compline,
    Zaxo

      Sorry, I guess I should have showed you the headers...

      I am using CGI.pm:  use CGI qw(:standard :cgi-lib escapeHTML);
      I guess I could just have the part where it prints the html link instead of putting &q=$_query I _could_ put a placeholder like &q={{replace_query}} then _after_ I Have it replace all the text then replace that with the original query. That just seems like a 'broken' way to do it. What do you think?

      thx,
      Richard

        Use CGI's html generation methods.

        # Assuming $q is from CGI->new my $url = $q->My::CGI::hypothetical_param( search => 'value' )->self_u +rl; sub My::CGI::hypothetical_param { my $current_q = shift; my $q = CGI->new( $current_q ); my $params = { @_ }; while ( my ( $param, $value ) = each %$params ) { if ( not defined $value ) { $q->delete ( $param ); } else { $q->param( $value ); } } $q; }
Re: Switching out text only in a variable
by NetWallah (Canon) on Nov 19, 2004 at 05:31 UTC
    The quick and dirty way would be to do a second, non-greedy replace, restoring the content of the tags - something like this (untested):
    s/<a (.+?)$__highlight_to_switch(.+?)>/<a $1$in{highlight_text}$2>/gi
    The not-so-dirty way is to use one of the HTML parsing modules, and replace only appropriate parts of the page in the first place.

        Earth first! (We'll rob the other planets later)