View Single Post
  #5 (permalink)  
Old 08-19-06, 11:19 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 7,572
Thanks: 5
Thanked 27 Times in 24 Posts
Well, it was more of an example. Give this a try:
PHP Code:
<?php

$search 
$_POST['search'];

if (
$search)
{
    
$file file('data.txt');
    
$keys preg_split('/[\s,]+/'preg_replace('/([^a-z0-9\s])/i'''trim($search) ) );
    
    if (
sizeof($keys) > 5)
    {
        die(
"Please enter less keywords");
    }
    else if (empty(
$keys[0]))
    {
        die(
"Please enter a valid keyword.");
    }
    
    
$pattern '/('implode('|'$keys) .')/i';
    
$matches = array();
    
    foreach (
$file as $line => $data)
    {
        list(
$month$day$year$info) = explode('|'trim($data) );
      
        if (
preg_match($pattern$info))
        {
            
$matches[$year][] = preg_replace($pattern'<span style="color:red;">$1</span>'$info);
        }
    }
    
    if (
sizeof($matches) > 0)
    {
        
ksort($matches);
        
        
printf('<p>%d match%s found.</p>'sizeof($matches), sizeof($matches) == NULL 'es');
        echo 
'<ol>';
    
        foreach (
$matches AS $year => $match)
        {
            echo 
'<li><font face="Times New Roman" size="2px">'$year .': 'current($match) .'</font></li>'"\n";
        }
        
        echo 
'</ol>';
    }
    else
    {
        echo 
'<p>No matches found.</p>';
    }
}

?> 
<form action="" method="post">
    <input type="text" name="search" />
    <input type="submit" value="Search" />
</form>

Last edited by nico_swd; 08-19-06 at 01:05 PM.
Reply With Quote