/    Sign up×
Community /Pin to ProfileBookmark

Sort By Relevance

Hi
This is a script which combines many different video feeds together, at the moment you can see that it

[code=php]shuffle($all);[/code]

instead of an algorithm to sort the records, i would like to change it so that it is sorted by relevancy to a string which i could specify. Here is the whole code for the page, and bellow that is an algoryhtym which i tried to implement into the code but would not work. Can anyone either help me implement this algorithm or write one that will work with this code?

[code=php]<?
function getall($searchterms) {
require(“yt.php”);
require(“gv.php”);
require(“dm.php”);
require(“mc.php”);

$yt = yt($searchterms);
$gv = gv($searchterms);
$dm = dm($searchterms);
$mc = mc($searchterms);

$all = array_merge($yt,$gv,$dm,$mc);
if(count($all)) {
include_once “latest.php”;
updatelatest(str_replace(“+”,”-“,$searchterms));
}
$bannedwords = “bannedwords.txt”;
$fp = fopen($bannedwords, “r”);
$wordstmp = fread($fp, filesize($bannedwords));
fclose($fp);
$words = explode(“n”,$wordstmp);
$words = array_map(“strtolower”,$words);

foreach($all as $index=>$arr) {
$arrtitle = $arr[‘title’];
foreach($words as $index2=>$word) {
if(strstr(strtolower($arrtitle),strtolower(str_replace(“n”,””,str_replace(“r”,””,$word))))) {
unset($all[$index]);
break;
}
}
}

srand((float)microtime() * 1000000);
shuffle($all);
foreach($all as $j=>$k) {
if(!trim($k[‘title’])||!trim($k[‘link’]))
unset($all[$j]);
}

return $all;
}

?>[/code]

This is the sort of algorythym

[code=php]usort($multifeed,’funnySort’);

function funnySort($a,$b)
{
// assign numerical value so we can sort on
// whether “term” is in title or content or neither

$term = ‘funny’;

$x = 0;
if (stripos($a->get_title(),$term))
$x = 2;
elseif (stripos($a->get_content(),$term))
$x = 1;

$y = 0;
if (stripos($b->get_title(),$term))
$y = 2;
elseif (stripos($b->get_content(),$term))
$y = 1;

return $x <= $y
}[/code]

i also tried to use this code on this site [url]http://www.webmastersherpa.com/tools/search.txt[/url]

Please can someone either help me adapt either of these algorithms or help me write one that will work with my code.

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @ojsimon spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...