2011-04-20 59 views
0

我正在製作搜索引擎,其中一項功能是搜索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("<", "&lt;", $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> 
+0

要指定,當我說「查詢」是第二部分時,我的意思是作爲一般的「在這裏插入查詢」類型的東西 – Matthew 2011-04-21 00:00:34

+1

「但是,我試圖在客戶端做出這種合理的嘗試已經完全失敗「 - 怎麼樣?爲什麼? – 2011-04-21 00:00:51

+0

哎呦,我的意思是服務器端。我只是修復了 – Matthew 2011-04-21 00:01:25

回答

0

我假設你是非常新的PHP,並從什麼地方,並試圖對其進行修改,以適應獲得這樣的腳本。

echo - 輸出一個或多個字符串 - 不返回任何值。

所以:

$txt2 = echo "$something" 

首先它失敗,因爲沒有結束分號。

將$東西打印到屏幕上,$ txt被設置爲無。

你確實已經在$東西的價值,所以我不知道爲什麼你會希望將其更改爲另一個變量,但是這將做到這一點:如果你想

$txt2 = $something; 

然後使腳本更好,大多數人都會指向DOMDocument

+0

其實是的,我是新來的PHP。儘管如此,我真的很想知道如何拼湊$ txt1,$ txt2和$ txt3以使http://www.google.com/search?hl=zh-CN&source=hp&biw=1542&bih=928&q=Query+&aq=f&aqi = g10&aql =&oq =&qscrl = 1 – Matthew 2011-04-21 00:15:26

+0

@Matthew它在回答中用'$ txt2 = $ something;'替換'$ txt2 = echo「$ something」'。儘管它是PHP的一個非常基礎的部分,但也許值得研究一些簡單的例子來首先理解語言。 – Jacob 2011-04-21 00:19:22

+0

我認識到我的錯誤,但是當我將它插回到我的網站時,它告訴我,我有一個意外的T_VARIABLE在線5 – Matthew 2011-04-21 00:21:16