乌云博客
专注互联网三十年

[php]自动更新google的hosts轻松访问谷歌

加个开机运行的任务计划,完美。。

 

<?php
$hostsFile = “C:/Windows/System32/drivers/etc/hosts”;
$RegExp = ‘/#google hosts [0-9]+ by 360kb.com[\s\S]+#google hosts [0-9]+ end/’;

$html = strip_tags(file_get_contents(‘http://www.360kb.com/kb/2_122.html’));

preg_match($RegExp, $html, $matchs);
$googleHosts = str_replace(‘&nbsp;’, ‘ ‘, $matchs[0]);
//echo $googleHosts;

$hosts = file_get_contents($hostsFile);

if(preg_match($RegExp, $hosts)){
$hosts = preg_replace($RegExp, $googleHosts, $hosts);
}else{
$hosts .= “\r\n\r\n”.$googleHosts.”\r\n\r\n”;
}

file_put_contents($hostsFile, $hosts);

分享到:更多 ()