PHP site Full-Text Search Code _php tutorial

Source: Internet
Author: User
Tags fread

Full text Search code in PHP station


If you are designing with Dreamweaver or FrontPage, they are there unless you deliberately delete them. 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 we can search The file is read and processed. The following is the reading of 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{
//Read file contents
$data =fread ("$dir/$file", "R"), FileSize ("$dir/$file");
Does 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 Web 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"));
Does not search itself
if ($file! = "search.php") {
//matches
if (eregi ("$keyword", $data)) {
Instead of
$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/845141.html www.bkjia.com true http://www.bkjia.com/PHPjc/845141.html techarticle PHP Site Full-Text Search code if you are designing with Dreamweaver or FrontPage, they are there unless you deliberately delete them. Let's take a step-by-step process and work on the project ...

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