2011-04-20 41 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