Miscellaneous
|
|
|
|
<html>
<head>
<title>
Search results for keyword=<? echo "$keyword" ?>
</title>
</head>
<body bgcolor=yellow text=green>
<hr>
<h2><b><u>Search results...</u></b></h2>
<hr>
<? if(!$keyword)
{
echo "Please enter keyword!<br>";
exit;
} $fl="keywords.txt"; $fp=fopen($fl, "r");
if(!fp)
{
echo "couldn't read file!";
} $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 "No matches were found!";
} ?>
The text file that this script searches looks like this:
search engine|http://www.yahoo.com|A Great search engine site - you can find almost anything there.
movies|http://movies.yahoo.com|Lists current movies, box office standings, and upcoming movies.
stocks|http://quote.yahoo.com|Great site to visit to check out stocks, bonds and foreign markets.
linux|http://www.linuxcentral.com|Great site if you want to buy some Linux stuff.
linux|http://www.linux.org|Lists tons of applications, documentation, news and etc.
linux|http://www.linuxtoday.com|Free linux news site. Lists current news.
zdnet|http://www.zdnet.com|Good site that lists news and has free downloads.
apache|http://www.apache.org|Most popular webserver in the world holding steady with 60% market share.
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
This is how the script works
1. Opens file that contains keywords, url, and description. Keywords, url and description are split using "|" character.
2. Feeds contents of the file into variable $fr.
3. Explode() $fr using endline "n". Output from explode() is stored into $line array variable.
4. $line is looped and each array element of line is exploded() using "|" element. Output from this explode() is assigned to $lin array variable.
5. $keyword variable is then compared to $lin[0] - since this variable holds keywords from the file.
6. If $keyword and $lin[0] matches - PHP outputs $lin[1], which is url, through <a href></a> tags. It also outputs description of the url as a $lin[2] variable.
7. Another cool element is $id variable. If no matches found between $keyword and $lin[0] variables, then $id will equal to number of lines, which constitutes no matches.
Another thing you really need is html file
<form method=post action=search.php>
<input type=text name=keyword>
<br>
<br>
<input type=submit value=Search>
</form>
I tested this script and it works great if correctly implemented. It should give you some ideas. You may use this code in any fashion you desire.
If you have better ideas, please, let me know (rusdmitriy@yahoo.com).
|
|
|
Usage Example
|
|
|
Rate This Script
|
|
|
|