Bug fixed, forgot to add some in. Try:
<?php
function keywordSearch(&$keywords)
{
global $keyword_count;
$lines = file('search_data.txt');
$results = array();
$symbols = array('[', ']', '-', '(', ')');
$replace = array('\[', '\]', '\-', '\(', '\)');
// colors for highlighting keywords
$colors = array( '#FF0000', // red
'#0000FF', // blue
'#99CC00', // green
'#CCCC00', // yellow
'#660066', // purple
'#FF0099' // pink
);
$i = 0;
foreach($lines as $line)
{
$keyword_found = false;
foreach($keywords as $key => $keyword)
{
if(!isset($keyword_count[$keyword]))
{
$keyword_count[$keyword] = 0;
}
$keyword = str_replace($symbols, $replace, $keyword);
if(eregi($keyword, $line))
{
$keyword_found = true;
// remove answer from end of line
$line = eregi_replace("\*([a-z0-9 ]+)", '?' , $line);
// highlight search keyword
$line = eregi_replace("($keyword)", "<span style=\"color: {$colors[$key]}; font-weight: bold;\">\\1</span>", $line);
$results[$i] = $line;
$keyword_count[$keyword]++;
}
}
if($keyword_found)
$i++;
}
return $results;
}
function displaySearchResults()
{
global $keyword_results, $keywords, $keyword_count;
$output = '<p>The keywords "<i><b>'. implode('</b></i>", "<i><b>', $keywords) . '</b></i>" found ' . count($keyword_results) . " result(s):\n";
if(is_array($keyword_results) && count($keyword_results) > 0)
{
$output .= "<ol>\n <li>" . implode("</li>\n <li>", $keyword_results) . "</li>\n</ol>\n";
}
echo "<p>$output</p>";
}
if(isset($_POST['submit']))
{
if(!empty($_POST['keyword']))
{
$_POST['keyword'] = str_replace(array("\r\n", "\r", "\n"), "\n", $_POST['keyword']);
$keywords = explode("\n", $_POST['keyword']);
$keyword_results = keywordSearch($keywords);
displaySearchResults();
}
else
{
echo 'Invalid search term';
}
}
?>
HTML
<form action="search.php" method="post">
Keywords:<br /><textarea name="keyword" cols="40" rows="4"><?php echo isset($_POST['keyword']) ? $_POST['keyword'] : null; ?></textarea>
<p><input type="submit" name="submit" value="Search" /></p>
</form>Also note that I changed the search box to a textarea rather than a text field so you'll need to modify your search box HTML code to the HTML code posted above. When performing a search you now place each keyword on a separate line within the Keywords search box rather than a space.