9999 Posted August 18, 2006 Share Posted August 18, 2006 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 Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted August 19, 2006 Share Posted August 19, 2006 [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. Quote Link to comment Share on other sites More sharing options...
9999 Posted August 19, 2006 Author Share Posted August 19, 2006 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. Quote Link to comment Share on other sites More sharing options...
Yesideez Posted August 19, 2006 Share Posted August 19, 2006 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. Quote Link to comment Share on other sites More sharing options...
9999 Posted August 20, 2006 Author Share Posted August 20, 2006 Ok but what if my $info contains 3 sentences? Would this work after each one or just the last one> Quote Link to comment Share on other sites More sharing options...
Jeremysr Posted August 20, 2006 Share Posted August 20, 2006 You could replace all ". "'s with ". "[code=php:0]$info = str_replace(". ", ". ", $info);[/code] Quote Link to comment Share on other sites More sharing options...
biotech Posted December 8, 2006 Share Posted December 8, 2006 did you make this to work,i mean the search thing..i need this badly too Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.