SitePoint Sponsor |
|
User Tag List
Results 1 to 5 of 5
-
Dec 9, 2003, 08:34 #1
- Join Date
- Aug 2002
- Location
- Somewhere in Cyberspace
- Posts
- 385
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Local search engine - preview result?
I'm trying to put together a search engine for a site. I can't use a db, because the information about each item is stored in a txt-file, so I need to search them instead. That part is working, together with a way of highlighting the search words.
The problem is the chunk of text I want to display.
I want it to cut in whole words, not just after X number of caracters. I also want it to start about 5 or so words before the first search word, (if there are words before it). I've been searching the forum, but no example deals with multiple search words where the content is taken from a text file.
This is the code so far:
PHP Code:<?php
//$keywords = strip_tags(trim($_POST['keywords']));
function search_find($keywords)
{
$key_array = explode(" ",$keywords);
foreach ($key_array as $word)
{
$words[] = preg_quote($word, '/');
}
$highlight_words = implode('|', $words);
$keys = implode("|",$key_array);
$keyword = str_pad($keys,strlen($keys)+2,"#",STR_PAD_BOTH);
$search_pattern = str_pad($keyword,strlen($keyword)+1,"i");
foreach (glob("*.txt") as $filename)
{
$filecontent = file_get_contents($filename);
if (preg_match_all($search_pattern,$filecontent,$match))
{
$start = strpos($filecontent,"<p>");
$content = strip_tags(substr($filecontent,$start));
$number = str_word_count($content); //I thought I might use this to find out where to start and end...
echo preg_replace("/($highlight_words)/i", '<strong>$1</strong>', $content).'...<br>';
echo '<br><a href="show_prod.php?prodid='.$filename.'">'.$filename.'</a><hr>';
}
}
}
search_find('word 2000');
?>
This script will also have options for an AND search, and not just an OR search as it is now. That part is easy to make the way the script is now. The whole script is case insensitive.
Please help me!Reptilian Feline
| www.reptilian-feline.net | Art | Tutorials | Pets | Music |
Webdesign the hard way...
-
Dec 9, 2003, 11:36 #2
- Join Date
- Oct 2003
- Location
- Germany
- Posts
- 2,195
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
See if my code in this thread helps you.
-
Dec 10, 2003, 05:17 #3
- Join Date
- Aug 2002
- Location
- Somewhere in Cyberspace
- Posts
- 385
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by Gaheris
I guess I was a little tired yesterday.
So... to all those out there who are trying to do the same thing... I give you the code for the function. You have to put together your own form though
PHP Code:$keywords = strip_tags(trim($_REQUEST['keywords']));
$search_type = $_REQUEST['search_type'];
function search_find($keywords,$search_type)
{
//Set numbers of words to pad the result with
$config = array('words' => array('before' => 5, 'after' => 20));
$key_array = explode(" ",$keywords);
//Get the words to highlight later
foreach ($key_array as $word)
{
$words[] = preg_quote($word, '/');
}
$highlight_words = implode('|', $words);
//Boolean AND type of search
if ($search_type == 'all')
{
$keys = implode(" ",$key_array);
}
//Boolean OR type of search
else
{
$keys = implode("|",$key_array);
}
//Get a proper search-string for preg_match_all
$keyword = str_pad($keys,strlen($keys)+2,"#",STR_PAD_BOTH);
//Make it case insensitive
$search_pattern = str_pad($keyword,strlen($keyword)+1,"i");
//Search all files with the txt-filetype
foreach (glob("*.txt") as $filename)
{
//Get the content of the file
$filecontent = file_get_contents($filename);
//See if the content maches any or all keywords
if (preg_match_all($search_pattern,$filecontent,$match))
{
//Start putting out the result on the first paragraph, or put in any other type of start code to look for
$start = strpos($filecontent,"<p>");
//Remove all the HTML and PHP
$content = strip_tags(substr($filecontent,$start));
//Divide content into words
$a_str = explode(' ', $content);
//Find the first keyword match
$key = array_search($match[0][0], $a_str);
//Set start and end padding
$start = (isset($a_str[$key - $config['words']['before']])) ? $key - $config['words']['before'] : 0;
$end = (isset($a_str[$key + $config['words']['after']])) ? $key + $config['words']['after'] : sizeof($a_str) - 1;
//Cut out portion of content to display
$a_ret = array_slice($a_str, $start, $end - $start);
//Put it together with ...
$s_ret = '...'.implode(' ', $a_ret).'...';
//Highlight all visible keywords
echo preg_replace("/($highlight_words)/i", '<strong>$1</strong>',$s_ret);
//This part can be ommited, but is useful for separating the files if different linktypes are to be displayed
if (($filename == 'image.txt') || ($filename == 'database.txt') || ($filename == 'desktop.txt'))
{
//Make the dynamic link
echo '<br><a href="show_cat.php?katid=';
//This part demonstrates a switch when filename doesn't equal the link id.
switch($filename):
case 'image.txt':
echo '1';
break;
case 'database.txt':
echo '2';
break;
case 'desktop.txt':
echo '3';
break;
endswitch;
//Close the link tag
echo '">Show</a><hr>';
}
else
{
//This part shows how the link works when filename is the same as id, and removes .txt from name.
echo '<br><a href="show_prod.php?prodid='.substr($filename,0,-4).'">Show</a><hr>';
}
}
}
}
Reptilian Feline
| www.reptilian-feline.net | Art | Tutorials | Pets | Music |
Webdesign the hard way...
-
Dec 10, 2003, 07:00 #4
- Join Date
- Oct 2003
- Location
- Germany
- Posts
- 2,195
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Glad my code could help you, looks like you put together some nice functionality in that function of yours.
-
Dec 10, 2003, 07:45 #5
- Join Date
- Aug 2002
- Location
- Somewhere in Cyberspace
- Posts
- 385
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
That's how I work
, trying to think of any possibility.
It has to be easy to use, look nice, and be easy to modify and update. The more coding I do, the more imåportant the updateability becomesReptilian Feline
| www.reptilian-feline.net | Art | Tutorials | Pets | Music |
Webdesign the hard way...
Bookmarks