View Single Post
  #7 (permalink)  
Old 08-19-06, 02:49 PM
9999 9999 is offline
Newbie Coder
 
Join Date: Jul 2006
Posts: 86
Thanks: 0
Thanked 0 Times in 0 Posts
I adapted your code to fit my needs (such as: I replaced the ordered list with a $count variable because I didn't want the indentation), however, I have 3 Questions.

1) I am not real familar to patern matching etc. In the code you gave me, is it restrictive in any way? What I mean is that for some searches I get less results than I dowith using the stristr as I did earlier.

2) In my first echo line, I want to display the month, year and date associated with that record. I tried this:
PHP Code:

echo '<center><b><font face="Times New Roman" size=2px">' $count'--' $month  '&nbsp;' $day ',&nbsp;' $year '</font></b></center>'
but it prints December 31 for every enrty. The year is correct though. What did I do wrong?

3) I want to restrict the search results to a number, say, 200. I play around with:
PHP Code:

if ((sizeof($matches) > 0) AND (sizeof($matches) < 200)) 

but that returns no values. How did I screw that up?


Here is a snippet of the complete code:
PHP Code:

  $file file('data.txt');


    
$keys preg_split('/[\s,]+/'preg_replace('/(^\s+|\#|\/|\'|\"|\~|\^|\.|\+|\$|\s+$)/'''$search) );
    
    
$count 0;

    if (
sizeof($keys) > 5)
    {
        die(
"Please enter less keywords");
    }

    
$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);
        }
    }

    
ksort($matches);

    if (
sizeof($matches) > 0)
    {
        
printf('<p>%d match%s found.</p>'sizeof($matches), sizeof($matches) == NULL 'es');

        foreach (
$matches AS $year => $match)
        {
            
$count++;
            echo 
'<center><b><font face="Times New Roman" size=2px">' $count'--' $month  '&nbsp;' $day ',&nbsp;' $year '</font></b></center>';
            echo 
'<font face="Times New Roman" size="2px">' current($match) .'</font>';
            echo 
"<p>";
        }
    }
    else
    {
        echo 
"No results were found for your search--Please try again";
    } 
Reply With Quote