Home >php教程 >PHP源码 >php获取百度关键词查询结果总数

php获取百度关键词查询结果总数

PHP中文网
PHP中文网Original
2016-05-25 17:10:32994browse

[PHP]代码    

<html>
<head>
<title>在线演示_php获取百度关键词查询结果总数</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="keywords" content="百度,baidu,查询结果">
<meta name="description" content="php获取百度关键词查询结果总数 by enenba.com">
</head>
<body>
<?php $k=isset($_POST[&#39;k&#39;])?$_POST[&#39;k&#39;]:&#39;&#39;; ?>
<form method="post" action="">
	查询关键词:
	<input type="text" name="k" size="50" value="<?php echo $k ?>">
	<input type="submit" value="提交">
</form>

<?php
/*
	获取百度关键词查询结果总数
	2012-7-26
	@param str $keyword 
	@param str 整型字符
 */
function getKeywordTotal($keyword) {
	$url = &#39;http://www.baidu.com/s?wd=&#39;.$keyword;
	$html = file_get_contents($url);
	$search = &#39;/<span class="nums"[^>]*?>[^<]*?([0-9,]*?)</i&#39;; //获取<span class="nums">中部分
	preg_match($search,$html,$match);
	$match = str_replace(&#39;,&#39;,&#39;&#39;,$match); //去逗号
	preg_match(&#39;/<span[^>]*>.*?(\d+)/&#39;,$match[0],$r);//得到最后的总数:
	return $r[1];
}
if($k==&#39;&#39;) {
	exit(&#39;请输入数据&#39;);
}else{
	echo &#39;共有&#39;.getKeywordTotal($k).&#39;个结果。&#39;;
}
?>
</body>
</html>

                   

                   

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:php文件上传。Next article:PHP模拟QQ登录