在本地上没什么问题,测试结果在附图里.
1. [代码][PHP]代码
<title>Search test</title><p>
</p><center>
<form>
<input><input><input><br><input>Google
<input>Baidu
<input>php
<input>mysql
<input>Java
<input>html
</form>
</center>
<?php $keywords=$_GET['keywords'];
$pn=$_GET['pn'];
$engine=$_GET['engine'];
if(empty($keywords))
die('Sorry,no keywords.');
//添加$keyword中加号
$keywords=str_replace(' ','+',$keywords);
//支持特殊字符
$keywords=urlencode($keywords);
/**
*切换搜索引擎
*/
if(($engine=='google')||(empty($engine))||($engine=='html')){
header("Content-type:text/html;charset=big5");//加个头
require('google.sty');
//temp variables
//$keywords='google pig';
//$pn=20;
if(($engine=='html')){
$keywords=$keywords.'+site:www.w3schools.com';
}
$url='http://www.google.com.hk/search?q='.$keywords.'&start='.$pn;
//echo $url;
//$contents=file_get_contents($url);
//try fopen() with google
//$fp=fopen($url,'r');
//while(!feof($fp)){
//$contents=$contents.fgets($fp,999);}
//try with curl
$contents=get_data($url);
$start=strpos($contents,'<p id="ires">');
//echo $start.'start<br>';
$end=strpos($contents,'<p></p>';
$inf=substr($contents,$start,$len);
//过滤掉google伪链接,替换搜索链接,尾部链接
$inf=str_replace('/url?q=','',$inf);
$inf=str_replace('search?q=','index.php?engine=google&keywords=',$inf);
//过滤google js带来的无用后缀
//$pattern='&sa=([a-z0-9_\-]){1}&ei=([a-z0-9_\-]){18,22}&ved=([a-z0-9_\-]){9,13}&usg=([a-z0-9_\-]){34}';
$pattern2='&sa=([a-z0-9_\-]){1}&ei=([a-z0-9_\-]){22}&ved=([a-z0-9_\-]){9,13}&usg=([a-z0-9_\-]){34}';
//test regular expression below
$inf=eregi_replace($pattern2,' ',$inf);
//$write='test writing ability';
//file_put_contents('/home/mageia/search_test',$inf);
//替换类似内容链接无法打开链接
$inf=str_replace('/search?','http://www.google.com/search?',$inf);
//替换google对链接的转换
$inf=urldecode($inf);
echo $inf;
$guid='<p><a>1</a>
<a>2</a>
<a>3</a>
<a>4</a>
<a>5</a>
<a>6</a>
<a>7</a>
<a>8</a>
<a>9</a>
<a>10</a>
<a>11</a>
<a>12</a>
</p>';
//$currentpage当前所在页
$currentpage=$pn/10+1;
$currentpos=strpos($guid,$currentpage.'');
//echo $start.'<br>';
//real start position
$start=strpos($contents,'',$start);
//real end position
$end=strpos($contents,'<p>',$start);
$len=$end-$start;
//echo $end.'<br>';
$inf=substr($contents,$start,$len);
//替换一些失效链接
$inf=str_replace('/s?wd=','index.php?engine=baidu&keywords=',$inf);
echo $inf;
$guid='</p><p><a>1</a>
<a>2</a>
<a>3</a>
<a>4</a>
<a>5</a>
<a>6</a>
<a>7</a>
<a>8</a>
<a>9</a>
<a>10</a>
</p>';
//$currentpage当前所在页
$currentpage=$pn/10+1;
$currentpos=strpos($guid,$currentpage.'';
//actual start position
$start+=8;
//echo $start.'<br>';
$start=strpos($phpcontents,'<p>');
$phpinf=substr($phpcontents,$start,$end-$start);
echo $phpinf;
}
elseif(($engine=='mysql')||($engine=='java')){
header("Content-type:text/html;charset=utf-8");//加个头
require('search-core.sty');
require('search.sty');
require('results.sty');
require('grids-ses.sty');
require('tabview-ses.sty');
require('treeview-ses.sty');
//enable page links to work
$pgstart=$pn+1;
$pgend=$start+9;
//url中不能有回车符
if($engine=='mysql'){
$url='http://search.oracle.com/search/search?&q='.$keywords.'&group=MySQL&search_startnum='.$pgstart.'&search_endnum='.$pgend.'&num=10';}
else{
$url='http://search.oracle.com/search/search?search_p_main_operator=all&group=Documentation&q='.$keywords.'+url:/javase'.'&search_startnum='.$pgstart.'&search_endnum='.$pgend.'&num=10';
}
//echo $url;
//$mysqlcontents=file_get_contents($url);
//try with curl
$mysqlcontents=get_data($url);
$start=strpos($mysqlcontents,'</p><p>');
$end=strpos($mysqlcontents,'</p><p>');
$mysqlinf=substr($mysqlcontents,$start,$end-$start);
echo $mysqlinf;
if($engine=='mysql'){
$guid='</p><p><a>1</a>
<a>2</a>
<a>3</a>
<a>4</a>
<a>5</a>
</p>';
}else{
$guid='<p><a>1</a>
<a>2</a>
<a>3</a>
<a>4</a>
<a>5</a>
</p>';
}
$currentpage=$pn/10+1;
$currentpos=strpos($guid,$currentpage.'
2. [图片]屏幕截图.png

3. [图片] 屏幕截图.png

4. [图片] 屏幕截图.png

5. [图片] 屏幕截图.png
6. 屏幕截图.png
7. [图片] 屏幕截图.png










