- 相關(guān)推薦
5個(gè)超級(jí)有用的php片段
盡管PHP經(jīng)常被人詬病,被人貶低,被人當(dāng)玩笑開(kāi),事實(shí)證明,PHP是全世界網(wǎng)站開(kāi)發(fā)中使用率最高的編程語(yǔ)言。PHP最大的缺點(diǎn)是太簡(jiǎn)單,語(yǔ)法不嚴(yán)謹(jǐn),框架體系很弱,但這也是它最大的優(yōu)點(diǎn),一個(gè)有點(diǎn)編程背景的普通人,只需要學(xué)習(xí)PHP半天時(shí)間,就可以上手開(kāi)始開(kāi)發(fā)web應(yīng)用了
一、黑名單過(guò)濾
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
二、隨機(jī)顏色生成器
復(fù)制代碼 代碼如下:
function randomColor() {
$str = '#';
for($i = 0 ; $i < 6 ; $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();
三、從網(wǎng)絡(luò)下載文件
復(fù)制代碼 代碼如下:
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');
五.從網(wǎng)頁(yè)提取關(guān)鍵詞
正如這小標(biāo)題所說(shuō)的那樣:這個(gè)代碼片段能讓你輕易地從網(wǎng)頁(yè)中提取元關(guān)鍵詞。
$meta = get_meta_tags('http://www.emoticode.net/');
$keywords = $meta['keywords'];
// Split keywords
$keywords = explode(',', $keywords );
// Trim them
$keywords = array_map( 'trim', $keywords );
// Remove empty values
$keywords = array_filter( $keywords );
print_r( $keywords );
【5個(gè)超級(jí)有用的php片段】相關(guān)文章:
超級(jí)實(shí)用的9個(gè)PHP代碼片段07-25
超級(jí)有用的PS技巧04-01
php學(xué)習(xí)之php配置07-15
PHP學(xué)習(xí):PHP拼音類(lèi)01-25
php正則去掉php注釋07-30
什么是PHP PHP與ASP比較03-11