php代码

php代码

第一,黑名单过滤

函数is_spam($text,$file,$split = ‘:’,$regex = false){

$handle = fopen($file,’ Rb ‘);

$contents = fread($handle,filesize($ file));

fclose($ handle);

$lines = explode(“n “,$ contents);$ arr = array();foreach($ lines as $ line){ list($ word,$count) = explode($split,$ line);if($ regex)$ arr[$ word]= $ count;else $ arr[preg _ quote($ word)]= $ count;} preg_match_all(“~ “)。内爆(‘ | ‘,array_keys($arr))。” ~ “,$text,$ matches);$ temp = array();foreach($ matches[0]as $ match){ if(!in_array($match,$ temp)){ $ temp[$ match]= $ temp[$ match]+1;if($ temp[$ match]& gt;= $arr[$word])返回true} }返回false} $ file = ‘ spam.txt$str = ‘此字符串有猫,狗字’;if(is_spam($str,$file))回显“这是垃圾邮件”;else回显“这不是垃圾邮件”;阿瑟:3只狗:3只猫:2只猴子:2

第二,随机颜色生成器

函数randomColor() {

$ str = ‘ #

for($ I = 0;$ i & lt6 ;$i++) {

$randNum = rand(0,15);

开关($randNum) {

案例10:$ rand num = ‘ A ‘;打破;

案例11:$ rand num = ‘ B ‘;打破;

案例12:$ rand num = ‘ C ‘;打破;

案例13:$ rand num = ‘ D ‘;打破;

案例14:$ rand num = ‘ E ‘;打破;

案例15:$ rand num = ‘ F ‘;打破;

}

$str。= $ randNum

}

返回$ str} $ color = random color();

第三,从网上下载文件

set _ time _ limit(0);//支持所有文件类型//此处URL:$ URL = ‘ http://som site . com/some _ video . flv ‘;$ pi = pathinfo($ URL);$ ext = $ pi[‘ extension ‘];$ name = $ pi[‘ filename ‘];//创建新的cURL资源$ ch = cURL _ init();//设置URL和其他合适的选项curl_setopt($ch,CURLOPT_URL,$ URL);curl_setopt($ch,CURLOPT_HEADER,false);curl_setopt($ch,CURLOPT_BINARYTRANSFER,true);curl_setopt($ch,CURLOPT_AUTOREFERER,true);curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);//抓取URL并传递给浏览器$ opt = curl _ exec($ ch);//关闭cURL资源,释放系统资源cURL _ close($ ch);$seFile = $name。’.’。$ extif(preg_match(“/[^0-9a-z._-]/i “,$ se file))$ se file = MD5(microtime(true))。’.’。$ ext$handle = fopen($seFile,’ WB ‘);fwrite($handle,$ opt);fclose($ handle);

第四,Alexa/Google页面排名

function page_rank($page, $type = ‘alexa’){ switch($type){ case ‘alexa’: $url = ‘http://alexa.com/siteinfo/’; $handle = fopen($url.$page, ‘r’); break; case ‘google’: $url = ‘http://google.com/search?client=nclient-auto&ch=6-1484155081&features=Rank&q=info:’; $handle = fopen($url.’http://’.$page, ‘r’); break; } $content = stream_get_contents($handle); fclose($handle); $content = preg_replace(“~(n|t|ss+)~”,”, $content); switch($type){ case ‘alexa’: if(preg_match(‘~<div class=”data (down|up)”><img.+?>(.+?) </div>~im’,$content,$matches)){ return $matches[2]; }else{ return FALSE; } break; case ‘google’: $rank = explode(‘:’,$content); if($rank[2] != ”) return $rank[2]; else return FALSE; break; default: return FALSE; break; } } // Alexa Page Rank: echo ‘Alexa Rank: ‘.page_rank(‘techug.com’); echo ‘ ‘; // Google Page Rank echo ‘Google Rank: ‘.page_rank(‘techug.com’, ‘google’);函数page_rank($page,$ type = ‘ Alexa ‘){ switch($ type){ case ‘ Alexa ‘:$ URL = ‘ http://Alexa . com/siteinfo/’;$handle = fopen($url。$page,’ r ‘);打破;case ‘ Google ‘:$ URL = ‘ http://Google . com/search?n client-auto & amp;ch=6-1484155081。特征=等级& ampq = info:’;$handle = fopen($url。http://’。$page,’ r ‘);打破;} $ content = stream _ get _ contents($ handle);fclose($ handle);$ content = preg _ replace(” ~(n | t | ss+)~ “,”,$ content);switch($ type){ case ‘ Alexa ‘:if(preg _ match(‘ ~ & lt;div class = ” data(down | up)” & gt;& ltimg。+?& gt(.+?)& lt/div & gt;~im ‘,$content,$ matches)){ return $ matches[2];}else{返回FALSE}破;case ‘google’: $rank = explode(‘:’,$ content);if($rank[2]!= ‘ ‘)返回$ rank[2];否则返回FALSE打破;默认值:返回FALSE打破;} } // Alexa页面排名:echo ‘Alexa排名:’。page _ rank(‘ techug . com ‘);回显“”;//Google Page Rank echo ‘ Google Rank:’。page_rank(‘techug.com ‘,’ Google ‘);

动词 (verb的缩写)强制下载文件

$ filename = $ _ GET[‘ file ‘];//从URL获取fileid查询文件ID $ Query = sprintf(” SELECT * FROM tableName WHERE ID = ‘ % s ‘ “,MySQL _ real _ escape _ string($ filename));$ SQL = MySQL _ query($ query);if(MySQL _ num _ rows($ SQL)& gt;0){ $ row = MySQL _ fetch _ array($ SQL);//设置一些headers header(” Pragma:public “);header(” Expires:0 “);header(” Cache-Control:must-revalidate,post-check=0,pre-check = 0 “);头(“内容类型:应用程序/强制下载”);报头(“内容类型:应用/八位字节流”);头(“内容类型:应用/下载”);header(“内容-处置:附件;文件名= “。basename($row[‘FileName’])。”;”);头(“内容-传输-编码:二进制”);header(“Content-Length:”。filesize($ row[‘ FileName ‘]);@ readfile($ row[‘ FileName ‘]);退出(0);} else { header(” Location:/”);退出;}

6.通过电子邮件显示用户的Grator头像。

$gratar_link = ‘http://www.gratar.com/atar/’ . md5($comment_author_email) . ‘?s=32’; echo ‘<img src=”‘ . $gratar_link . ‘” />’;$ gratar _ link = ‘ http://www . gratar . com/atar/’。md5($comment_author_email)。?s = 32’;回声’ & ltimg src= ” “。$gratar_link。”/>;;

第七,用cURL获得RSS订阅。

$ ch = curl _ init();curl_setopt($ch,CURLOPT_URL,’ https://feedburner . Google . com/API/awareness/1.0/GetFeedData?id = 7 qkrmib 4 r 9 rscbplq 5 qgadiiq 4’);curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,2);$ content = curl _ exec($ ch);$ subscribers = get _ match(‘/circulation = “(。*)”/isU ‘,$ content);curl _ close($ ch);八、时差计算功能前($ TERM) {

$periods = array(“秒”、”分”、”小时”、”日”、”周”、”月”、”年”、”十年”);$lengths = array(“60 “,” 60 “,” 24 “,” 7 “,” 4.35 “,” 12 “,” 10 “);$ now = time();$ difference = $ now-$ time;$ tense = ” agofor($ j = 0;$ difference & gt= $ lengths[$ j]& amp;& amp$ j & lt计数(长度)-1;$ j++){ $ difference/= $ lengths[$ j];} $ difference = round($ difference);如果($差!= 1) { $periods[$j]。= ” s}返回“$ difference $ periods[$ j]’ ago ‘”;}

八、截取图片

$ filename = ” test.jpglist($w,$h,$type,$ attr)= getimagesize($ filename);$ src _ im = imagecreatefromjpeg($ filename);$ src _ x = ‘ 0//begin x $ src _ y = ‘ 0 ‘;//begin y $ src _ w = ‘ 100 ‘;//width $ src _ h = ‘ 100 ‘;//height $ dst _ x = ‘ 0 ‘;//destination x $ dst _ y = ‘ 0 ‘;//destination y $ dst _ im = imagecreatetruecolor($ src _ w,$ src _ h);$ white = imagecolorlallocate($ dst _ im,255,255,255);imagefill($dst_im,0,0,$ white);imagecopy($dst_im,$src_im,$dst_x,$dst_y,$src_x,$src_y,$src_w,$ src _ h);头(” Content-type:image/png “);imagepng($ dst _ im);image destroy($ dst _ im);

九、检查网站是否宕机。

功能访问($url){

$agent = “Mozilla/4.0(兼容;MSIE 5.01windows NT 5.0)”;$ ch = curl _ init();curl_setopt ($ch,CURLOPT_URL,$ URL);curl_setopt($ch,CURLOPT_USERAGENT,$ agent);curl_setopt ($ch,CURLOPT_RETURNTRANSFER,1);curl_setopt ($ch,CURLOPT_VERBOSE,false);curl_setopt($ch,CURLOPT_TIMEOUT,5);curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);curl_setopt($ch,CURLOPT_SSLVERSION,3);curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);$ page = curl _ exec($ ch);//echo curl _ error($ ch);$httpcode = curl_getinfo($ch,curl info _ HTTP _ CODE);curl _ close($ ch);if($httpcode & gt;= 200 & amp& amp$ httpcode & lt300)返回true否则返回false} if(访问(” http://www.google.com”)) echo“网站OK”。”n”;else echo“网站宕机”;

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。

发表回复

登录后才能评论