<?php
# nubsearch
# a prototype newbie interface for advanced search optionz

function google_url($q,$p) {
	return "http://www.google.com/search?num=100&start=".(100*$p)."&q=".urlencode($q);
}

function my_url($q) {
	return $_SERVER['PHP_SELF']."?q=".urlencode($q);
}

$max = $_REQUEST['max']?$_REQUEST['max']:1;
for($i=0;$i<$max;$i++) {
	$pages[$i] = join("",file(google_url($_REQUEST['q'],$i)));
}

$page = join(" ", $pages);

preg_match_all("|<p class=g><a href=(.*?)>(.*?)</a>.*?<font size=-1>(.*?)<br>|s", $page, $matches);

$urls = $matches[1];
$titles = $matches[2];
$blurbs = $matches[3];

$strings = join(" ", array_merge($titles, $blurbs));
$strings = strip_tags($strings);
$strings = strtolower($strings);
$strings = preg_replace("/&.{2,5};/"," ", $strings);
$strings = preg_replace("/\s+/"," ", $strings);
$words = explode(" ", $strings);
$words = preg_replace("/\W*(.*?)\W*$/","$1", $words);
function filt($w) { return strlen($w)>1; }
$words = array_filter($words, "filt");
$commonz = explode(" ","the of to and a in is it you that he was for on are with as i his they be at one have this from or has by but what some we out other were all there when up use your how said an each she which do if will way about many then them would like so these her long make things see him two has look more day could go come did no most my over know than first who down side been now find any new get made where after back only game every me oour under very just from say much before too also why again such their here not our page pages can cannot");
$words = array_diff($words, $commonz);

foreach($words as $w) {
	$freq[$w]++;
}

arsort($freq);
$freq = array_slice($freq, 0,20);

ksort($freq);

echo "<html><body>";
foreach($freq as $w=>$c) {
	echo "<span style=\"font-size: ".(5*sqrt($c))."px\"><a href=\"".my_url($w)."\">$w</a></span><br />";
}
echo "</body></html>";
?>
