Hi there, I am very new to perl and I am trying to get a search script running. So far it is working as expected however one thing is really bugging me, the search results are sadly full of un-encoded ampersands....yuk, the rest of the site is fully valid XHTML 1.0 strict, so this sort of throws a proverbial spanner in the works. I have managed to kill off some of the ampersands but there are still a couple of illusive ones that my perl knowledge just cant grapple with. This is the output that i get:
<div><strong>Search results (8 found, 8 shown)</strong><p> 1 - <a href="fcp.pl?words=water+and+fountain&wt=ew&bl=or&amp;d=/link</ +a><br> 2 - <a href="fcp.pl?words=water+and+fountain&wt=ew&bl=or&amp;d=/link</ +a><br> 3 - <a href="fcp.pl?words=water+and+fountain&wt=ew&bl=or&amp;d=/link</ +a><br> 4 - <a href="fcp.pl?words=water+and+fountain&wt=ew&bl=or&amp;d=/link</ +a><br> 5 - <a href="fcp.pl?words=water+and+fountain&wt=ew&bl=or&amp;d=/link</ +a><br> </div>
As you can see there is one encoded ampersand in each link but two un-encoded ampersands. The script that is making these ugly little varmints looks like this: (this is just a snippet)
if(exists $form{'words'}) { $search_words = $form{'words'}; if($form{'wt'} eq 'be') {$search_wb = ' checked'; $search_ew = '';} else {$search_wb = ''; $search_ew = ' checked';} if($form{'bl'} eq 'an') {$search_bAND = ' checked'; $search_bOR = ''; +$search_bPHR = '';} elsif($form{'bl'} eq 'ph') {$search_bAND = ''; $search_bOR = ''; $sear +ch_bPHR = ' checked';} else {$search_bAND = ''; $search_bOR = ' checked'; $search_bPHR = '';} $wl = lc $search_words; $wl =~ tr/a-z0-9/ /c; $wl =~ s/(\A\s+)|(\s+\Z)//g; @words = split /\s+/, $wl; if($wl eq '' || $#words < 0) { $extra = "<font color=red>Please enter some words in the search box. +</font><br>"; @words = (); } else { $title = join ' ', 'Search results for', @words; } $search_q = $ENV{'QUERY_STRING'}; $search_q =~ s/\&amp;pg=\d+//; } else { $search_words = ''; $search_wb = ' checked'; $search_ew = ''; $search_bAND = ''; $search_bOR = ' checked'; $search_bPHR = '';
etc etc.. As far as I can tell the problem is the "$search_bAND" thing. Problem is how do I change "AND" to & ? Any thoughts or suggestions are much appreciated thanks. Humble Regards Nathan.

20041009 Edit by castaway: Changed title from 'New to perl'


In reply to Parsing un-encoded ampersand in XHTML by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.