Home >Backend Development >PHP Tutorial >PHP search engine based on text files

PHP search engine based on text files

不言
不言Original
2018-04-14 17:03:472071browse

This article mainly introduces the PHP search engine based on text files, which has certain reference value. Now I share it with everyone. Friends in need can refer to it

PHP based on text files Search engine

<span style="color:#000000">The following is the text-based PHP search engine source code I found from zend.com and made some trivial Modification: <br>search.php <br><br></span>
<html>  
<head>  
<title>  
    搜索关键字是——<? "$keyyword" ?>  
</title>  
</head>  
<body bgcolor=yellow text=green>  
<form method=post action=search.php>  
<input type=text name=keyword>  
<br>  
<br>  
<input type=submit value=搜索>  
</form> 
<hr>  
<h2><b><u>Search results...</u></b></h2>  
<hr>  
<?  
if(!$keyword)  
    {  
    echo "请输入关键字!<br>";  
    exit;  
    }  
$fl="keywords.txt"; //保存搜索数据的文本文件 
$fp=fopen($fl, "r");  
if(!fp)  
    {  
    echo "无法读取数据文件!!";  
    }  
$fr=fread($fp, filesize($fl));  
$line=explode("/n", $fr);  
$id=0;  
for($i=0; $i<count($line); $i++)  
{  
    $lin=explode("|", $line[$i]);  
    if(eregi("$keyword", "$lin[0]"))  
        {  
        echo "<a href=$lin[1]>$lin[1]</a><br><spacer type=horizontal size=40>$lin[2]<br><br>";  
        }  
    else  
        {  
        $id++;  
        }  
}  
if($id==count($line))  
    {  
    echo "找不到匹配的结果!";  
    }  
?> 
</body> 
</html>
<span style="color:#000000">The format of the text file keywords containing search data is as follows: <br><br>Search engine | http://www.yahoo.com|Great search engine, you can find almost everything here. <br>movies|http://movies.yahoo.com|Popular movies, box office receipts, and upcoming new ones Movies. <br>Stocks|http://quote.yahoo.com|You can watch the stock market and foreign markets. <br>linux|http://www.linuxcentral.com|If you want to buy some Linux products, Why not come here. <br>linux|http://www.linux.org|Lots of applications, articles and more. <br>linux|http://www.linuxtoday.com|Linux news site, listing the latest LINUX news. <br>zdnet|http://www.zdnet.com|News and download station. <br>apache|http://www.apache.org|Popular website server. <br><br> <br>This code runs like this: <br><br>1. Open the text file containing the data. Keywords, URL, and introduction are all separated by |; <br>2. Assign the contents of this file to the variable $fr; <br>3. The value output by explode() is assigned to the $line variable; <br>4. Use explode() to repeatedly process the $line variable and assign the elements separated by | to the lin[] array; <br>5. Then compare the keyword with lin[0]; <br>6. If it matches, output - PHP will output lin[1], because this is a URL, so the cfba799dd43fce78ec3e9e3d200b7e485db79b134e9f6b82c0b36e0489ee08ed tag should be added; <br>7. Another very cool variable is $id. If the keyword does not match lin[0], it will be equal to &line with $id, and then it will tell you that it cannot be found:) <br><br>In oso me I also saw another similar piece of code, the implementation methods are similar. <br></span>

Related recommendations:

php reads a text file and outputs the most used lines in that line line by line Detailed explanation of method examples of characters and corresponding times

How to print variables in text files during PHP debugging

The above is the detailed content of PHP search engine based on text files. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn