📄 00000066.htm
字号:
going to change the output of our search in any way.
<BR>
<BR>$query =~ s/([-+i.<>&|^%=])/\\\1/g;
<BR>
<BR>Boy does that look messy! That is basically just a Regular Expression to <BR>escape all of the meta characters. Basically this will
<BR>change a + into a \+.
<BR>
<BR>Now we need to move right along and open up our target document. When we do <BR>this, we will need to read the entire file into one
<BR>variable. Then we will work from there.
<BR>
<BR>open (SEARCH, "$doc");
<BR>undef $/;
<BR>$text = <SEARCH>;
<BR>close (SEARCH);
<BR>
<BR>The only thing you may not be familiar with is the undef $/; statement you <BR>see there. For our search to work correctly, we
<BR>must undefine the Perl variable that separates the lines of our input file. <BR>The reason this is necessary is due to the fact that we
<BR>must read the entire file into one variable. Unless this is undefined, only <BR>one line will be read.
<BR>
<BR>Now we will start the output of the results page. It is good to customize it <BR>and make it appealing somehow to the user. This is free
<BR>form HTML so all you HTML guys, go at it.
<BR>
<BR>Now we will do the real searching job. Here is the meat of our search. You <BR>will notice there are two commented regular
<BR>expressions in the search. If you want to not display any images or show any <BR>links, you should uncomment those lines.
<BR>
<BR>@records = split(/$recdelim/,$text);
<BR>
<BR>We want to split up the file into an array of records. Each record is a valid <BR>search result, but is separate from the rest. This is
<BR>where the record delimiter comes into play.
<BR>
<BR>foreach $record (@records)
<BR>{
<BR># $record =~ s/<a.*<\/a>//ig; # Do not print links inside this
<BR># doc.
<BR># $record =~ s/<img.*>//ig; # Do not display images inside this
<BR># doc.
<BR> if ( $record =~ /$query/i ) {
<BR> print $record;
<BR> $matches++;
<BR> }
<BR>}
<BR>
<BR>This basically prints out every $record that matches our search criteria. <BR>Again you can change the number of search criterion
<BR>you use by changing that if statement to something like this.
<BR>
<BR>if ( ($record =~ /$query/i) && ($record =~ /$anotheritem/) ) {
<BR>
<BR>This will try to match both queries with $record and upon a successful match, <BR>it will dump that $record to our results page.
<BR>Notice how we also increment a variable called $matches every time a match is <BR>made. This is not as much as to tell the user
<BR>how many different records were found, but more of a count to tell us if no <BR>matches were found so we can tell the user that no,
<BR>the system is not down, but in fact we did not match any records based upon <BR>that query.
<BR>
<BR>Now that we are done searching and displaying the results of our search, we <BR>need to do a few administrative actions to ensure
<BR>that we have fully completed our job.
<BR>
<BR>First off, as I was mentioning before, we need to check for zero matches in <BR>our search and let the user know that we could not
<BR>find anything to match his query.
<BR>
<BR>if ($matches eq "0") {
<BR> $query =~ s/\\//g;
<BR>
<BR>print << "End_Again";
<BR>
<BR> <center>
<BR> <h2>Sorry! "$query" was not found!</h2><p>
<BR> </center>
<BR>End_Again
<BR>}
<BR>
<BR>Notice that lovely Regular Expression. Now that we had to take all of the <BR>trouble to escape those meta characters, we need to
<BR>remove the escape chars. This way when they see that their $query was not <BR>found, they will not look at it and say ``But that is
<BR>not what I entered!'' Then we want to dump the HTML to disappoint the user.
<BR>
<BR>The only two things left to do is end the HTML document cleanly and allow for <BR>the back link.
<BR>
<BR>if ( $backlink ne "" ) {
<BR> print "<center>";
<BR> print "<h3><a href=\"$backlink\">Go
<BR>back</a></h3>";
<BR> print "</center>";
<BR>}
<BR>
<BR>print << "End_Of_Footer";
<BR>
<BR></body>
<BR></html>
<BR>
<BR>End_Of_Footer
<BR>
<BR>All done. Now you are happy because the user is happy. Not only have you <BR>streamlined your website by allowing to search a
<BR>single page, but you have increased the user's utility by giving them the <BR>results they want. The only result of this is more hits. By
<BR>helping your user find the information he needs, he will tell his friends <BR>about your site. And his friends will tell their friends and so
<BR>on. Putting the customer first sometimes does work!
<BR>
<BR> <BR>-- <BR> 白马带著她一步步的回到中原。白马已经老了,只能慢慢的走, <BR>但终是能回到中原的。江南有杨柳、桃花,有燕子、金鱼…… <BR>汉人中有的是英俊勇武的少年,倜傥潇洒的少年……但这个美 <BR>丽的姑娘就像古高昌国人那样固执: <BR> <BR> 「那都是很好很好的,可是我偏不喜欢。」 <BR> <BR>※ 来源:·BBS 水木清华站 bbs.net.tsinghua.edu.cn·[FROM: 202.99.18.67] <BR><CENTER><H1>BBS水木清华站∶精华区</H1></CENTER></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -