Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Hey all, I wondered if anyone colud help me, I need a basic search function.
To search a list of terms with there explainations from a .txt file and output snippets of the content ( 5 terms at a time).
An example of the .txt file would be:--
#------apple
A great fruit with many varieties.
#------apple/GrannySmith
A type of Apple
#------Orange
Another fruit with looks like exactly what it says on the tin.
etc... (with many more terms)
and my code for the search so far is:-Code: Select all
<?php
//
// Class for finding a key in a multi-dimensional array
// This is specific to my needs, not general
//
class key_search
{
var $found = array();
// Constructor method
function key_search($search, $array = '', $case = 0)
{
// Set up the parameters
$search = (!is_array($search)) ? array($search) : $search;
$array = (!is_array($array)) ? array($array) : $array;
$ereg = ($case) ? 'ereg' : 'eregi';
//
// Search through the array keys for a match
//
// Loop through keys
foreach (array_keys($array) as $key)
{
// Loop through search targets
foreach ($search as $target)
{
// Return false if an empty string
if ($target == '')
{
return false;
}
// If target expression matches the key and the resolved key in array exists
if ($ereg($target, $key) && $array[$key][0])
{
// Put the resolved key in the list
$this->found[] = $array[$key][0];
}
}
}
// Loop through values
foreach ($array as $value)
{
// If the value is an array, search in it
if (is_array($value))
{
$this->key_search($search, $value, $case);
}
}
}
}
//
// Class for getting an array of tags from a file
//
class file_tags
{
var $list = array();
var $blocks = array();
// Constructor method
function file_tags($file, $marker = '', $separator = '/')
{
// Get the raw contents of the file
$contents = file_get_contents($file);
// Explode the file into pieces using this special marker
$this->blocks = explode($marker, $contents);
// Start breaking the blocks down (ignore the first block, just extra)
for ($i = 1; $i < count($this->blocks); $i++)
{
// Explode the tags into an array
$tags = explode($separator, trim(substr($this->blocks[$i], 0, strpos($this->blocks[$i], "\n"))));
// Create a string that looks like an array
$string = '';
foreach ($tags as $tag)
{
$string .= "['$tag']";
}
$string .= "[] = $i";
// Evaluate the string as a keyset and attach to $list
eval("\$this->list$string;");
}
}
}
// Search keywords
$keywords = stripslashes(trim($_POST['search']));
// Search form
echo '<form method="post" >
Search for an Article: <input type="text" name="search" value="' . $keywords . '">
<input type="submit" name="submit" value="Submit">
</form>';
// Create a new tag list
$file = new file_tags('terms.txt', '#------');
// Search for keywords in the list
$search = new key_search($keywords, $file->list);
?>
<pre>
<?php
//
// Display
//
// Display found matches
print_r($search->found);
// Loop through found matches
foreach ($search->found as $found)
{
// Extract the tag for this match
$tag = trim(substr($file->blocks[$found], 0, strpos($file->blocks[$found], "\n")));
// Display the tag and its blocked content
echo "\nTag: $tag\n" . strstr($file->blocks[$found], "\n");
'</a><div style="margin-top:5px;margin-bottom:2em;border:1px black solid;">';
}
// Display the list
print_r($file->list);
?>
</pre>vee
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]