Jump to content

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!

Edited by joecooper
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.

Edited by PaulRyan

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.

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.