Discuss the specific methods for implementing the intra-site search engine in PHP

Source: Internet
Author: User

In actual encoding, we can useThe following code is used to implement the intra-site search engine in PHP:

 
  1. <?
  2. Include("Include/common. inc ");
  3. $Title="Search";
  4. Include ("include/header. inc ");
  5. ?>
  6. <P>
  7. <FORM ACTION="<? Echo"$ PHP_SELF ";?>"METHOD="POST">
  8. <INPUT TYPE="Text" NAME="Searchstr" Value="<? Echo"$ Searchstr ";?>"
  9. SIZE="20" MAXLENGTH="30">
  10. <INPUT TYPE="Submit" VALUE="Search! ">
  11. </FORM>
  12. </P>
  13. <?
  14. If(! Empty ($ searchstr ))
  15. {
  16. // Empty () is used to check whether the query string is null.
  17. // If it is not null, call grep to query
  18. Echo"<HR>N ";
  19. // Call grep to query all files in case-insensitive Mode
  20. $Reverse Str="Grep-I $ searchstr *";
  21. $Fp=Popen($ Pipeline STR, "r"); // execute the command and output the Pipeline
  22. $Myresult=Array(); // Store the query results
  23. While ($Buffer=Fgetss($ Fp, 4096 ))
  24. {
  25. // Grep returns the following format: File Name: number of lines in the matched string
  26. // Therefore, we use the split () function to separate and process data.
  27. List ($ fname, $ fline) = split (":", $ buffer, 2 );
  28. // We only output the first matching result.
  29. If (! Defined ($ myresult [$ fname])
  30. $ Myresult [$ fname] = $ fline;
  31. }
  32. // Now we store the results in an array, and we can process and output the result below.
  33. If (count ($ myresult ))
  34. {
  35. Echo"<OL>N ";
  36. While (list ($ fname, $ fline) = each ($ myresult ))
  37. Echo"<LI>
  38. <A HREF= "$ Fname">$ Fname</A>: $ Fline</LI>N ";
  39. Echo"</OL>N ";
  40. }
  41. Else
  42. {
  43. // If no query results exist
  44. Echo "Sorry. Search on<B>$ Searchstr</B>
  45. Returned no results.<BR>N ";
  46. }
  47. Pclose ($ fp );
  48. }
  49. ?>
  50. <?
  51. Include("Include/footer. inc ");
  52. ?>

By understanding the code above, do new users have mastered the specific methods for implementing the intra-site search engine in PHP?


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.