Jump to content

Archived

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

9999

Improving my search script

Recommended Posts

This piece of code searches the $info field of my delimited text file database:

[code]foreach ($file as $line => $data)
{
    list($month, $day, $year, $info) = explode('|', trim($data) );
    if (stristr($info, $search))
    {
    echo $info;
    }
}[/code]
I have 3 Questions:

1) Lets say a person was searching for "John Smith" they could enter "John Smith" or "John" or "Smith" and it would work.  I would like to improve my search so that if a person searched for "John Smith" it would also find results for "John T. Smth" or " John Q. Smith" etc.  Is there an easy way to do this?

2)  How can I sort the $year field?

3)  For each record, the data in my $info is 2 or 3 sentences long.  When the current script returns a result, there is only 1 space between sentences even though there are 2 in my database.  Any suggestions on how I can fix this?

Thanks in advance

Share this post


Link to post
Share on other sites
[quote]
1) Lets say a person was searching for "John Smith" they could enter "John Smith" or "John" or "Smith" and it would work.  I would like to improve my search so that if a person searched for "John Smith" it would also find results for "John T. Smth" or " John Q. Smith" etc.  Is there an easy way to do this?
[/quote]

I would say to break the string apart and then do a search for both words and only return the results that have both in them, regarless of order or what's between them.

[quote]2)  How can I sort the $year field?[/quote]

You have to create a multidimensional array, then sort it.  You can create a normal array, then use array_multisort, or you can create an array that has the year as the keys, and then sort the keys.  Just be careful with the latter because duplicate keys will over write eachother.

[quote]3)  For each record, the data in my $info is 2 or 3 sentences long.  When the current script returns a result, there is only 1 space between sentences even though there are 2 in my database.  Any suggestions on how I can fix this?[/quote]

What do you mean it only returns one space but there are two in the db?  Please clarify.

Share this post


Link to post
Share on other sites
In my database, for a given record, the $info field might contain multiple sentences that have the standard 2 spaces after the period.  When that record is extracted and echoed, there is only one space after the period.  It does it in every case.  I thought the trim function was causing it, but it does it even when I remove it.

Share this post


Link to post
Share on other sites
Browsers will automatically remove multiple spaces and replace them with one single space. What you need to do is replace the two spaces with   (non-break space) if you want those two spaces to appear.

Browsers treat the non-break space as text even though you can't see it so you can do this:
[code]echo $info.' ';[/code]
That way the browser will replace the two spaces with one single space and the non-break space will be tagged onto the end giving the illusion of two spaces.

Share this post


Link to post
Share on other sites
Ok but what if my $info contains 3 sentences?  Would this work after each one or just the last one>

Share this post


Link to post
Share on other sites
You could replace all ". "'s with ".  "

[code=php:0]$info = str_replace(". ", ".  ", $info);[/code]

Share this post


Link to post
Share on other sites

×

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.