Do a self-search engine _php Tutorial

Source: Internet
Author: User
Ccterran (original)

Author: iwind

Friends with Dreamweaver made a website, no dynamic content, just some personal collection of articles, personal introduction and so on. Now more content, want to ask me to help him to do a search engine. To be honest, it was an easy question, so I made one. Now I see in other forums that some people want to do this, so I would like to talk about this knowledge, to understand the method.

Before writing the program to think of a good idea, the following is my idea, who may have better, but note that this is only a method problem: Traverse all the files  read the content  search keyword, if the match is put into an array  reading group. Before implementing these steps, I assume that your page is standard and has a title (), there are ( ), if you are using Dreamweaver or FrontPage design, then unless you deliberately delete, they are in existence. Let's step through it and improve the search engine in the project.

One, design a search form
Build a search.htm in the root directory of the site, as follows:


Search Form





Second, the search program
Then build a search.php file in the root directory to handle the data from the Search.htm table only son. The content is as follows
Get search Keywords
$keyword =trim ($_post["keyword"]);
Check whether it is empty
if ($keyword = = "") {
echo "The keyword you are searching for cannot be empty";
exit;//End Program
}
?>

This allows you to make a prompt if the keyword entered by the visitor is empty. The following is a traversal of all files.

We can iterate through all the files in a recursive way, either with the function opendir,readdir or with the PHP directory class. We now use the former.
Functions that traverse all files
function Listfiles ($dir) {
$handle =opendir ($dir);
while (false!== ($file =readdir ($handle))) {
if ($file! = "." && $file! = "...") {
If it's a directory, continue searching.
if (Is_dir ("$dir/$file")) {
Listfiles ("$dir/$file");
}
else{
Here to handle the
}
}
}
}

?>

In the Scarlet letter where we can read to the file, processing. The following is reading the contents of the file, and check whether the content contains the keyword $keyword, if it contains the file address assigned to an array.
!--? php //$dir is the search directory, $keyword is the search keyword, $array is the stored array
function listfiles ($dir, $keyword,& $array) {
$handle =opendir ($dir);
while (false!== ($file =readdir ($handle))) {
if ($file! = "." && $file! = "...") {
if (Is_dir ("$dir/$file")) {
Listfiles ("$dir/$file", $keyword, $array);
}
else{
//reads the contents of the file
$data =fread (fopen ("$dir/$file", "R"), FileSize ("$dir/$file"));
//Do not search itself
if ($file! = "search.php") {
//matches
if (eregi ("$keyword", $data)) {
$array []= "$dir/$file";
}
}
}
}
}
}
//define array $array
$array =array ();
Execute function
Listfiles (".", "PHP", $array);
Print the search results
foreach ($array as $value) {
echo "$value".
\ n ";
}
?

Now combine this result with the beginning of a program, enter a keyword, and then you will find the relevant results in your site are searched. We are now perfecting it.
1, list the title of the content
Put
if (eregi ("$keyword", $data)) {
$array []= "$dir/$file";
}
Change into
if (eregi ("$keyword", $data)) {
if (Eregi ("(.+)", $data, $m)) {
$title = $m ["1"];
}
else{
$title = "no title";
}
$array []= "$dir/$file $title";
}
The principle is that if you find it in the file contentsXxx, then take xxx out as the title, if not found then the title is not "no title".

2, search only the subject section of the content of the Web page.
There will be a lot of HTML code in the page, and these are not what we want to search, so we want to remove them. I'm now using regular expressions and strip_tags, and I can't get rid of all of them.
Put
$data =fread (fopen ("$dir/$file", "R"), FileSize ("$dir/$file"));
Do not search for itself
if ($file! = "search.php") {
is matched
if (eregi ("$keyword", $data)) {
Switch
$data =fread (fopen ("$dir/$file", "R"), FileSize ("$dir/$file"));
if (eregi (" ]+) > (. +)", $data, $b)) {
$body =strip_tags ($b ["2"]);
}
else{
$body =strip_tags ($data);
}
if ($file! = "search.php") {
if (eregi ("$keyword", $body)) {

3, add link to the title
foreach ($array as $value) {
echo "$value". "
\ n ";
}
Change into
foreach ($array as $value) {
Apart
List ($filedir, $title) =split ("[]", $value, "2");
Output
echo "$value". "
\ n ";
}
4 Preventing timeouts
If the file is more, it is necessary to prevent the PHP execution time timeout. Can be added to the file header
Set_time_limit ("600");
In seconds, so the above is set to 10 minutes limit.


So the complete program is
Set_time_limit ("600");
Get search Keywords
$keyword =trim ($_post["keyword"]);
Check whether it is empty
if ($keyword = = "") {
echo "The keyword you are searching for cannot be empty";
exit;//End Program
}
function Listfiles ($dir, $keyword,& $array) {
$handle =opendir ($dir);
while (false!== ($file =readdir ($handle))) {
if ($file! = "." && $file! = "...") {
if (Is_dir ("$dir/$file")) {
Listfiles ("$dir/$file", $keyword, $array);
}
else{
$data =fread (fopen ("$dir/$file", "R"), FileSize ("$dir/$file"));
if (Eregi (" ]+) > (. +) ", $data, $b)) {
$body =strip_tags ($b ["2"]);
}
else{
$body =strip_tags ($data);
}
if ($file! = "search.php") {
if (eregi ("$keyword", $body)) {
if (Eregi (" (.+)", $data, $m)) {
$title = $m ["1"];
}
else{
$title = "no title";
}
$array []= "$dir/$file $title";
}
}
}
}
}
}
$array =array ();
Listfiles (".", "$keyword", $array);
foreach ($array as $value) {
Apart
List ($filedir, $title) =split ("[]", $value, "2");
Output
echo "$title". "
\ n ";
}
?>

So far, you have done your own search engine, you can modify the Content Processing section to improve it, you can search for the title, or search for content functions. You can also consider paging. Leave it to yourself.

This shows that using Preg_match instead of eregi will be much quicker. This is just for easy access, so the usual eregi are used.

http://www.bkjia.com/PHPjc/314558.html www.bkjia.com true http://www.bkjia.com/PHPjc/314558.html techarticle Ccterran (original) Author: Iwind Friends with Dreamweaver made a website, no dynamic content, just some personal collection of articles, personal introduction and so on. Now the content is more ...

  • 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.