Web开拓者必备的10个救命的PHP代码片断

时间:2014-02-15所属栏目: PHP/PHP框架 来源: 未知 热度:

[代码] 要害词高亮

function highlight($sString, $aWords) {  
    if (!is_array ($aWords) || emptyempty ($aWords) || !is_string ($sString)) {  
        return false;  
    }  
 
    $sWords = implode ('|', $aWords);  
    return preg_replace ('@\b('.$sWords.')\b@si', '<strong style="background-color:yellow">$1</strong>', $sString);  
} 


[代码] 获取你的Feedburner的用户

function get_average_readers($feed_id,$interval = 7){  
    $today = date('Y-m-d', strtotime("now"));  
    $ago = date('Y-m-d', strtotime("-".$interval." days"));  
    $feed_url="https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=".$feed_id."&dates=".$ago.",".$today;  
    $ch = curl_init();  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
    curl_setopt($ch, CURLOPT_URL, $feed_url);  
    $data = curl_exec($ch);  
    curl_close($ch);  
    $xml = new SimpleXMLElement($data);  
    $fb = $xml->feed->entry['circulation'];  
 
    $nb = 0;  
    foreach($xml->feed->children() as $circ){  
        $nb += $circ['circulation'];  
    }  
 
    return round($nb/$interval);  
} 

[代码] 自动天生暗码

function generatePassword($length=9, $strength=0) {  
    $vowels = 'aeuy';  
    $consonants = 'bdghjmnpqrstvz';  
    if ($strength >= 1) {  
        $consonants .= 'BDGHJLMNPQRSTVWXZ';  
    }  
    if ($strength >= 2) {  
        $vowels .= "AEUY";  
    }  
    if ($strength >= 4) {  
        $consonants .= '23456789';  
    }  
    if ($strength >= 8 ) {  
        $vowels .= '@#$%';  
    }  
 
    $password = '';  
    $alt = time() % 2;  
    for ($i = 0; $i < $length; $i++) {  
        if ($alt == 1) {  
            $password .= $consonants[(rand() % strlen($consonants))];  
            $alt = 0;  
        } else {  
            $password .= $vowels[(rand() % strlen($vowels))];  
            $alt = 1;  
        }  
    }  
    return $password;  
} 

[代码] 压缩多个CSS文件

header('Content-type: text/css');  
ob_start("compress");  
function compress($buffer) {  
  /* remove comments */ 
  $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);  
  /* remove tabs, spaces, newlines, etc. */ 
  $buffer = str_replace(array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), '', $buffer);  
  return $buffer;  
}  
 
/* your css files */ 
include('master.css');  
include('typography.css');  
include('grid.css');  
include('print.css');  
include('handheld.css');  
 
ob_end_flush(); 

[代码] 获取短网址

function getTinyUrl($url) {  
    return file_get_contents("http://tinyurl.com/api-create.php?url=".$url);  
} 
标签:
评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)