Jump to content

Improving my search script


9999

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
Link to comment
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.
Link to comment
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.
Link to comment
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.
Link to comment
Share on other sites

  • 3 months later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.