我有这个非常简单的搜索脚本,但想要一个链接到术语结果的搜索页面如果我在Google搜索汽车我得到这个页面 https://www.google.com/#q=cars 现在这个脚本只返回同一页面中的搜索结果
<?php
$source_dir = ".";
$results = array();
if(get_magic_quotes_gpc()==1) $_POST["q"]=stripslashes($_POST["q"]);
$criteria = strtolower($_POST["q"]);
function check_criteria($filename,$criteria){
if($criteria=='' or $criteria=='""'){
return false;
}
else{
$criteria1=$criteria;
if(substr($criteria,0,1)=='"' and substr($criteria,-1)=='"'){
$criteria1=substr($criteria,1,-1);
if(strpos($filename,$criteria1)!==false){
return true;
}
}
elseif(strpos($criteria,' ')!==false){
$arr=explode(' ',$criteria);
if($arr) foreach($arr as $val){
$criteria1=trim($val);
if(strpos($filename,$criteria1)!==false){
return true;
}
}
}
else{
if(strpos($filename,$criteria1)!==false){
return true;
}
}
}
}
function find_files($mydir){
global $results, $criteria;
if(($tdir=@opendir($mydir))!==false){
while($f=readdir($tdir)){
if($f!="." and $f!=".."){
if(is_dir($mydir."/".$f)){
if(find_files($mydir."/".$f)>=1000) return count($results);
}
elseif(is_file($mydir."/".$f)){
if(check_criteria(strtolower($f),$criteria)){
// found!
if(count($results)>=1000) return count($results);
$results[]=$mydir."/".$f;
}
}
}
}
closedir($tdir);
}
return count($results);
}
function format_filesize($size,$dec=1) {
$kb = 1024; // Kilobyte
$mb = 1024 * $kb; // Megabyte
$gb = 1024 * $mb; // Gigabyte
$tb = 1024 * $gb; // Terabyte
if($size < $kb) {
return $size." bytes";
}
else if($size < $mb) {
return round($size/$kb,$dec)." kb";
}
else if($size < $gb) {
return round($size/$mb,$dec)." mb";
}
else if($size < $tb) {
return round($size/$gb,$dec)." gb";
}
else {
return round($size/$tb,$dec)." tb";
}
}
?>
<html>
<head>
<title>Search Files</title>
<style>
.err {
color: #f00;
}
label {
font-weight: bold;
}
th {
background-color: #ccc;
font-weight: bold;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head>
<body bgcolor="#000000" text="#FFFFFF">
<h1> </h1>
<form method="post">
<label for="q">search:</label>
<input type="text" name="q" id="q" value="<?php echo htmlspecialchars($_POST["q"]) ?>" size="32" maxlength="128"> <input type="submit" name="search" value=" Search! ">
</form>
<?php
// Search results
if($_POST["search"]){
echo '<hr>'."\n\n";
echo '<h3>Search results:</h3>';
if(strlen($_POST["q"])<1){
echo '<p class="err">Please enter keyword.</p>';
}
elseif(strlen($_POST["q"])<2){
echo '<p class="err">Keyword is too short.</p>';
}
else{
$n=find_files($source_dir);
if($n<=0){
echo '<p>No results found.</p>';
}
elseif($n>=1000){
echo '<p>Too much results, please refine your search. Displaying first <b>100</b> results:</p>';
}
else{
echo '<p><b>'.$n.'</b> files found:</p>';
}
if($results){
echo '<table width="500" cellspacing="0" cellpadding="3" border="0">'."\n";
echo '<tr><th align="left">File Name</th><th align="right">Size</th></tr>';
foreach($results as $val){
$size=filesize($val);
echo '<tr><td align="left" valign="top" nowrap><a href="'.$val.'" target="_blank">'.basename($val).'</a> </td><td align="right" valign="top" nowrap>'.format_filesize($size).'</td></tr>'."\n";
}
echo '</table>'."\n";
}
}
}
?>
</body>
</html>