Jump to content

Search script / pattern matching question


9999

Recommended Posts

This script excepts a user input and searches for it in the $info field of my delimited text file database. If a user enters multiple words it will search for the occurance of each word. My question is what would I change in my script to make it search for exactly what was typed in? I want to allow my users to first search for exactly what they type in and if the are not happy with the number of results returned, I will give them the opportunity to do a "deep search" which is what I already have.
[code]<?php
$months = array (
    'January' => '01',
    'February' => '02',
    'March' => '03',
    'April' => '04',
    'May' => '05',
    'June' => '06',
    'July' => '07',
    'August' => '08',
    'September' => '09',
    'October' => '10',
    'November' => '11',
    'December' => '12',
);
if($_REQUEST['usersearch'] == '1')  // Display Keyword Search Results
{
    $search = $_GET['search'];

    $file = file('data.txt');
    $search = preg_replace('/([^a-z0-9\s])/i', '', trim($search) );
   
    if ($_GET['deep'])
    {
        $keys = preg_split('/[\s,]+/', $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';
    }
    else
    {
        $pattern = '/('. $search .')/';
    }
   
    $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)
    {
        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 toconvert 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  . '&nbsp;' . $day . ',&nbsp;' . $year . '--' .'</font></b>';
                echo '<font face="Times New Roman" size="2px">' . $result .'</font>';
                echo "<p>";

                if ($count >= 200)
                {
                    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>';
    }

}
?> 

<form action="" method="get">
  <input type="text" name="search" size="20">
  <input type="hidden" name="usersearch" value="1"/>
  <input type="checkbox" name="deep" value="1" /> Deep
  <input type="submit" value="Search"/>
</form>[/code]

My focus is on this line:
    [code]$pattern = '/('. $search .')/';[/code]

I have even changed it to:
    [code]$pattern = '/(\s'. $search .')+[\s\,\.]/';[/code]with no luck.

What would give me the equivalent of what the "stristr" function would return? For example, a search for "Rick Smith" would find "[color=red]Rick Smith[/color]" "[color=red]Rick Smith[/color]s" or "[color=red]Rick Smith[/color]erman" but not "Ricky Smith" or "Rick A. Smith"
Link to comment
https://forums.phpfreaks.com/topic/25716-search-script-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.