|
CS479/579 - Web Programming II
|
Displaying ./code/Async-example/words.php
<?php
function R($n) {
return rand() % $n;
}
$match = isset($_GET['match'])? $_GET['match'] : null;
$words = Array();
$f = fopen("/usr/dict/words", "r");
for($i=0; $s = fgets($f); $i++) {
$words[$i] = trim($s);
}
if ($match) {
$len=strlen($match);
$ret = Array();
$ridx = 0;
for($i=0; $i < count($words); $i++) {
if (strncmp($words[$i],$match,$len) == 0) {
$ret[$ridx++] = $words[$i];
if ($ridx > 9) break;
}
}
} else {
srand();
$ret = $words[R(count($words))];
}
echo json_encode($ret);
?>
|