我正在製作搜索引擎,其中一項功能是搜索Google等功能。我做的第一件事就是了解如何僅基於URL生成Google搜索。那麼,「http://www.google.com/search?hl=zh-CN & source = hp & biw = 1542 & bih = 928 & q =」是第一部分,「查詢」是第二部分,而「& aq = f & aqi = g10 & aql = & oq = & qscrl = 1「是第三部分。現在,在客戶端,我製作了一個名爲「搜索」的表單,其操作是下面的文件。但是,我在服務器端做出這種似是而非的嘗試完全失敗了。以下是代碼:呼應變量
<?php
$something = $_REQUEST["search"];
$txt1="http://www.google.com/search?hl=en&source=hp&biw=1542&bih=928&q=";
$txt2 = $something;
$txt3="&aq=f&aqi=g10&aql=&oq=&qscrl=1";
$string=$txt1.$txt2."+".$txt3;
$link_to_dig = $string;
$original_file = @file_get_contents($link_to_dig);
if(!$original_file)
die("Error loading {$link_to_dig}");
$path_info = parse_url($link_to_dig);
$base = $path_info['scheme'] . "://" . $path_info['host'];
$stripped_file = strip_tags($original_file, "<a>");
$fixed_file = preg_replace("/<a([^>]*)href=\"\//is", "<a$1href=\"{$base}/", $stripped_file);
$fixed_file = preg_replace("/<a([^>]*)href=\"\?/is", "<a$1href=\"{$link_to_dig}/?", $fixed_file);
preg_match_all("/<a(?:[^>]*)href=\"([^\"]*)\"(?:[^>]*)>(?:[^<]*)<\/a>/is", $fixed_file, $matches);
//DEBUGGING
//$matches[0] now contains the complete A tags; ex: <a href="link">text</a>
//$matches[1] now contains only the HREFs in the A tags; ex: link
$result = print_r($matches, true);
$result = str_replace("<", "<", $result);
print "<pre>" . $result . "</pre>";
?>
感謝您的幫助!
這是形式:
<form class="searchengine" name="search" action="search.php" method="post">
<input type="text" style="width:580px; height:35px; color:#000000;" class="search" name="search" />
</form>
要指定,當我說「查詢」是第二部分時,我的意思是作爲一般的「在這裏插入查詢」類型的東西 – Matthew 2011-04-21 00:00:34
「但是,我試圖在客戶端做出這種合理的嘗試已經完全失敗「 - 怎麼樣?爲什麼? – 2011-04-21 00:00:51
哎呦,我的意思是服務器端。我只是修復了 – Matthew 2011-04-21 00:01:25