Created
December 3, 2024 12:00
-
-
Save jeankassio/de9895a14f31d518e4c77f3ab883b0ef to your computer and use it in GitHub Desktop.
Advanced Search - Lite Version
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @param {string} $search | |
* @param {array} $datas | |
* @return {array} | |
*/ | |
public function search($search, $datas){ | |
$response = []; | |
if(is_null($search)){ | |
return $response; | |
} | |
$words = array_filter(explode(" ", $search), function ($word){ | |
return strlen($word) > 2; | |
}); | |
$basecount = count($words); | |
foreach($datas as $data){ | |
$points = 0; | |
if(!is_null($data)){ | |
$_data = explode(" ", $data); | |
similar_text(strtolower($data), strtolower($search), $psimil1); | |
if($psimil1 > 50){ | |
$points += ($psimil1 * 10); | |
} | |
foreach($_data as $_d){ | |
foreach($words as $word){ | |
similar_text(strtolower($_d), strtolower($word), $psimil2); | |
if($psimil2 > 50){ | |
$points += ($psimil2 / count($_data)); | |
} | |
if(str_contains(strtolower($data), strtolower($word))){ | |
$points += 150; | |
} | |
if(strtolower($_d) == strtolower($word)){ | |
$points += 1500; | |
} | |
} | |
} | |
} | |
if($points >= ($basecount * 400)){ | |
$response[] = [ | |
'points' => $points, | |
'item' => $data | |
]; | |
} | |
} | |
usort($response, function($a, $b) { | |
return $b['points'] - $a['points']; | |
}); | |
return array_column($response, 'item'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment