这篇文章主要介绍了10个超级有用值得收藏的php代码片段,本文讲解了黑名单过滤、随机颜色生成器、从网络下载文件、强制下载文件、通过email显示用户的gravatar头
尽管php经常被人诟病,被人贬低,被人当玩笑开,事实证明,php是全世界网站开发中使用率最高的编程语言。php最大的缺点是太简单,语法不严谨,框架体系很弱,但这也是它最大的优点,一个有点编程背景的普通人,只需要学习php半天时间,就可以上手开始开发web应用了。
网上有人总结几种编程语言的特点,我觉得也挺有道理的:
复制代码 代码如下:
php 就是: quick and dirty
java 就是: beauty and slowly
ruby 就是: quick and beauty
python 就是: quick and simple
在php的流行普及中,网上总结出了很多实用的php代码片段,这些代码片段在当你遇到类似的问题时,粘贴过去就可以使用,非常的高效,非常的省时省力。将这些程序员前辈总结出的优秀代码放到自己的知识库中,,是一个善于学习的程序员的好习惯。
一、黑名单过滤
复制代码 代码如下:
function 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(~.implode('|', 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] >= $arr[$word])
return true;
}
}
return false;
}
$file = 'spam.txt';
$str = 'this string has cat, dog word';
if(is_spam($str, $file))
echo 'this is spam';
else
echo 'this is not spam';
ab:3
dog:3
cat:2
monkey:2
二、随机颜色生成器
复制代码 代码如下:
function randomcolor() {
$str = '#';
for($i = 0 ; $i $randnum = rand(0 , 15);
switch ($randnum) {
case 10: $randnum = 'a'; break;
case 11: $randnum = 'b'; break;
case 12: $randnum = 'c'; break;
case 13: $randnum = 'd'; break;
case 14: $randnum = 'e'; break;
case 15: $randnum = 'f'; break;
}
$str .= $randnum;
}
return $str;
}
$color = randomcolor();
三、从网络下载文件
复制代码 代码如下:
set_time_limit(0);
// supports all file types
// url here:
$url = 'http://somsite.com/some_video.flv';
$pi = pathinfo($url);
$ext = $pi['extension'];
$name = $pi['filename'];
// create a new curl resource
$ch = curl_init();
// set url and other appropriate options
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);
// grab url and pass it to the browser
$opt = curl_exec($ch);
// close curl resource, and free up system resources
curl_close($ch);
$savefile = $name.'.'.$ext;
if(preg_match(/[^0-9a-z._-]/i, $savefile))
$savefile = md5(microtime(true)).'.'.$ext;
$handle = fopen($savefile, 'wb');
fwrite($handle, $opt);
fclose($handle);
四、alexa/google page rank
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=navclient-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('~
(.+?)
~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');
五、强制下载文件
复制代码 代码如下: