Jump to content

Search though a string array?


joecooper

Recommended Posts

Hi,

 

I've just spent the last several hours speeding though coding a sudden idea I had. Now I'm feeling a bit brain dead and cannot get my head around the next part.

 

I have a string array, results[0] which is full of file names.  I need to create a quick way to search though those names, and list in order of relativity / order of closest match. The search term would normally be more than 1 word, so the ones that match all the words, then going on to the ones that match fewer words etc.

 

Although there are around 100 - 200 strings in the array, I would only need to display the top 5 results.

 

Any help would be appriated.

 

Thanks

 

Joe!

Link to comment
https://forums.phpfreaks.com/topic/276103-search-though-a-string-array/
Share on other sites

Try something like this:

<?PHP

  $searchTerm = 'search';
 
  $items = array('test search.jpg', 'test.jpg', 'search.png', 'love.png', 'testicle.png', 'looool.php');
 
  foreach($items AS $file) {
      similar_text($searchTerm, $file, $percent);

      $final[$file] = $percent;
  }
 
  arsort($final);
 
  $final = array_slice($final, 0, 5);
 
  echo '<pre>';
  print_r($final);
 
?>

 

*Edit - Added array_slice to get first 5 elements.

Hi,  Thanks for that! seems to be what I were looking for.

 

Just that it outputs like this:

 

Array(    [http://www.SOMESITE.net/Eminem/Encore/07 Puke.mp3] => 20.3389830508    [http://www.SOMESITE.net/Eminem/Encore/06 Mosh.mp3] => 20.3389830508    [http://www.SOMESITE.net/Eminem/Relapse/06 Hello.mp3] => 19.6721311475    [http://www.SOMESITE.net/Eminem/Relapse/04 Insane.mp3] => 19.3548387097    [http://www.SOMESITE.net/Eminem/Relapse/03 My Mom.mp3] => 19.3548387097)

 

Possible for it to just output the URL?

If you put the value in the value, then you can't sort by the percentage comparison.

 

There is was to do a callback function, which I can't remember how.

 

array_keys, I have a feeling something like that existed, didn't get a good chance to look through the array functions.

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.