Why O why must we complicate such simple things? If you just want the count result of a search only, here is the complete working code:
<head>
<meta name="Author" content="Kevin Rubio">
<title>PHP Altavista Search</title>
</head>
<body>
<form method="POST" action="<? echo $PHP_SELF ?>">
<input type="text" name="klink" size="20"><input type="submit" value="Search" name="submit">
</form>
<?
altavista($klink); # Engages function
function altavista($klink) {
if ($klink){
$url="http://www.altavista.com/sites/search/res_text?sc=on&hl=on&amb=txt&q=$klink&kl=XX&stq=$startat";
$fcontents = file ($url);
while (list ($line_num, $line) = each ($fcontents)) {
if (eregi("We found ([, 0-9]*) results", $line, $arr)) { # if 0
print"<b>SEARCH FOR $klink returned $arr[1]</b><p>";
continue;
} # End if 0
if (preg_match ("/no document matching your query/i", "$line")) {
print "No match was found for <b>$klink</b>.";
exit;
}
}# End while
} # End if submit
} # End Altavista Function
?>
</body>