In actual encoding, we can useThe following code is used to implement the intra-site search engine in PHP:
- <?
- Include("Include/common. inc ");
- $Title="Search";
- Include ("include/header. inc ");
- ?>
- <P>
- <FORM ACTION="<? Echo"$ PHP_SELF ";?>"METHOD="POST">
- <INPUT TYPE="Text" NAME="Searchstr" Value="<? Echo"$ Searchstr ";?>"
- SIZE="20" MAXLENGTH="30">
- <INPUT TYPE="Submit" VALUE="Search! ">
- </FORM>
- </P>
- <?
- If(! Empty ($ searchstr ))
- {
- // Empty () is used to check whether the query string is null.
- // If it is not null, call grep to query
- Echo"<HR>N ";
- // Call grep to query all files in case-insensitive Mode
- $Reverse Str="Grep-I $ searchstr *";
- $Fp=Popen($ Pipeline STR, "r"); // execute the command and output the Pipeline
- $Myresult=Array(); // Store the query results
- While ($Buffer=Fgetss($ Fp, 4096 ))
- {
- // Grep returns the following format: File Name: number of lines in the matched string
- // Therefore, we use the split () function to separate and process data.
- List ($ fname, $ fline) = split (":", $ buffer, 2 );
- // We only output the first matching result.
- If (! Defined ($ myresult [$ fname])
- $ Myresult [$ fname] = $ fline;
- }
- // Now we store the results in an array, and we can process and output the result below.
- If (count ($ myresult ))
- {
- Echo"<OL>N ";
- While (list ($ fname, $ fline) = each ($ myresult ))
- Echo"<LI>
- <A HREF= "$ Fname">$ Fname</A>: $ Fline</LI>N ";
- Echo"</OL>N ";
- }
- Else
- {
- // If no query results exist
- Echo "Sorry. Search on<B>$ Searchstr</B>
- Returned no results.<BR>N ";
- }
- Pclose ($ fp );
- }
- ?>
- <?
- Include("Include/footer. inc ");
- ?>
By understanding the code above, do new users have mastered the specific methods for implementing the intra-site search engine in PHP?