大多数博主都努力在 Google 上列出尽可能多的页面。在 Google 中列出的好处可能包括:
- 增加博客访问量
- 增加广告点击次数
- 扩大访问者/受众群体
- 增加文章评论
- 增加推荐收入
使用少量的 PHP 代码,您可以查询 Google 以检索您的域在 Google 中列出的页面数。
代码
/* return result number */
function get_google_results($domain = 'davidwalsh.name')
{
// get the result content
$content = file_get_contents('http://www.google.com/search?q=site:'.$domain);
// parse to get results
$result = get_match('/Results <b>(.*)from/isU',$content);
// split the results
$split1 = explode('of about',$result);
// return result
return $split1[1] ? strip_tags($split1[1]) : 0;
}
/* helper: does the regex */
function get_match($regex,$content)
{
preg_match($regex,$content,$matches);
return $matches[1];
}
用法
/* do it! */
echo 'davidwalsh.name: '.get_google_results('davidwalsh.name'); // 164
echo 'digg.com: '.get_google_results('digg.com'); // 3,790,000
echo 'google.com: '.get_google_results('google.com'); // 19,300,000
echo 'cnn.com: '.get_google_results('cnn.com'); // 2,180,000
echo 'imdb.com: '.get_google_results('imdb.com'); // 19,000,000
echo 'dzone.com: '.get_google_results('dzone.com'); // 484,000
echo 'fark.com: '.get_google_results('fark.com'); // 7,390
echo 'some-domain-that-doesnt-exist.com: '.get_google_results('some-domain-that-doesnt-exist'); // 0
