I am creating a word search but I want to rank them base on the highest existence of search keyword. How can I solve this problem?
I am trying to make a search if array 1 key exists inside array 2 long string and then order the array by total occurrence of array 1 in array 2.
Blow is my code
$str = "Hello World January Jude";
$arr1 = ["Hello World January Jude Lol Love","Hello Lol Loop","Love Life Jude","Crude Flash Hello"];
$str = explode(" ", $str);
echo sort_base($arr1, $str);
function sort_base($arr, $str){
$count = "";
foreach ($arr as $valuer){
foreach ($str as $value){
//$list[] = strpos($valuer, $value, 0);
$count .= strpos($valuer, $value, 0)."<hr/>";
}
}
$arr = trim($count," ");
echo $arr;
}
Example input:
$array = ["Say Hello","Hello World"," Hello World Cup Final","Hello Cup","Hello","World"];
$str = "Hello World Cup";
Desire output:
Array in order:
- Hello World Cup
- Final Hello World
- Hello Cup
- Hello
- World
- Say Hello