尽管php经常被人诟病,被人贬低,被人当玩笑开,事实证明,php是全世界网站开发中使用率最高的编程语言。php最大的缺点是太简单,语法不严谨,框架体系很弱,但这也是它最大的优点,一个有点编程背景的普通人,只需要学习php半天时间,就可以上手开始开发web应用了。
网上有人总结几种编程语言的特点,我觉得也挺有道理的:
在php的流行普及中,网上总结出了很多实用的php代码片段,这些代码片段在当你遇到类似的问题时,粘贴过去就可以使用,非常的高效,非常的省时省力。将这些程序员前辈总结出的优秀代码放到自己的知识库中,是一个善于学习的程序员的好习惯。
一、黑名单过滤
$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
// 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('~<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');
五、强制下载文件
@readfile($row['filename']);
exit(0);
}else{
header("location: /");
exit;
}
$now = time();
$difference = $now - $time;
$tense = "ago";
for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
$difference /= $lengths[$j];
}
$difference = round($difference);
if($difference != 1) {
$periods[$j].= "s";
}
return "$difference $periods[$j] 'ago' ";
}
$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 = imagecolorallocate($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);
header("content-type: image/png");
imagepng($dst_im);
imagedestroy($dst_im);
如对本文有疑问, 点击进行留言回复!!
*HDU1024.Max Sum Plus Plus(DP+滚动数组优化)
荐 「PHP」不依赖集成环境,使用Nginx手动配置Thinkphp运行环境
网友评论