Jump to content

Pattern matching question


9999

Recommended Posts

At this time, I have a flat (delimited) text file being used for my database. I am wondering what changes can be made to my existing code listed below to achieve my objective.

 

If someone searched for "Rick A Smith" this script finds every instance of each word, which is fine. The only problem is that everytime the character "A" is found in the entire database, it shows as a result even when it is part of another word. For example: James, Richard, Mark, etc.

 

How could I restrict the findings of "a" for example to only be found when there is a space before and after it like the example above: "Rick A Smith"

 

Code Snippet:

 

$months = array ( 
    'January' => '1', 
    'February' => '2', 
    'March' => '3', 
    'April' => '4', 
    'May' => '5', 
    'June' => '6', 
    'July' => '7', 
    'August' => '8', 
    'September' => '9', 
    'October' => '10', 
    'November' => '11', 
    'December' => '12', 
    ); 

    $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(); 
    $matchcount = 0; 

    foreach (array_values($file) AS $data) 
    { 
        list($month, $day, $year, $info) = explode('|', trim($data) ); 

        if (preg_match($pattern, $info)) 
        { 
            $matches["$year {$months[$month]} $day"][] = preg_replace($pattern, '<span style="color:red;">$1</span>', $info); 
            $matchcount++; 
        } 
    } 



    if (sizeof($matches) > 0) 
    { 
        if($_REQUEST['sort'] == 'year') 
        { 
             ksort($matches); 
        } 
        $count = 0; 

        printf('<center><b><p>%d match%s found.</p></b></center>', $matchcount, $matchcount == 1 ? NULL : 'es'); 
        $months = array_flip($months);                              // flip array to convert months back to names 
        foreach ($matches AS $days => $event) 
        { 
            foreach ($event AS $result) 
            { 
                list($year, $month, $day ) = explode(' ', $days ); 
                $month  = $months[$month];                        // convert back to month name 
                $count++; 

                echo '<b><font face="Times New Roman">' . $count . ') ' . $month  . ' ' . $day . ', ' . $year . '--' .'</font></b>'; 
                echo '<font face="Times New Roman" size="2px">' . $result .'</font>'; 
                echo "<p>"; 

                if ($count >= 200) 
                { 
                    echo '<center><b>Results displayed limited to 200.</b></center>'; 
                    break; 
                } 

            } 

            if ($count >= 200) 
            { 
                break; 
            } 
        } 
        printf('<center><b><p>%d match%s found.</p></b></center>', $matchcount, $matchcount == 1 ? NULL : 'es'); 
    } 
    else 
    { 
        echo '<p><center><b>No results were found for your search--Please try again.</b></center></p>'; 
    }

Link to comment
https://forums.phpfreaks.com/topic/135697-pattern-matching-question/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.