10个典型实用的PHP代码片段

本文将介绍10个经常会用到的PHP代码片段,包括黑名单过滤、随机颜色生成器、从网上下载文件、Alexa/Google Page Rank、强制下载文件、用Email显示用户的Gravator头像、用cURL获取RSS订阅数、截取图片、检查网站是否宕机。

一、黑名单过滤

    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'; 

二、随机颜色生成器

    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();

三、从网上下载文件

    set_time_limit(0); 
    // 支持所有文件类型
    // URL
    $url = 'http://www.hoehub.com/usr/uploads/2016/10/781925596.zip'; 
    $pi = pathinfo($url); 
    $ext = $pi['extension']; 
    $name = $pi['filename']; 

    // 创建一个新的CURL资源
    $ch = curl_init(); 

    // 设置网址和其他适当的选项 
    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); 

    // 抓取网址并将其传递给浏览器
    $opt = curl_exec($ch); 

    // 关闭资源
    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');

五、强制下载文件

    $filename = $_GET['file']; //Get the fileid from the URL 
    // Query the file ID 
    $query = sprintf("SELECT * FROM tableName WHERE id = '%s'",mysql_real_escape_string($filename)); 
    $sql = mysql_query($query); 
    if(mysql_num_rows($sql) > 0){ 
        $row = mysql_fetch_array($sql); 
        // Set some headers 
        header("Pragma: public"); 
        header("Expires: 0"); 
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
        header("Content-Type: application/force-download"); 
        header("Content-Type: application/octet-stream"); 
        header("Content-Type: application/download"); 
        header("Content-Disposition: attachment; filename=".basename($row['FileName']).";"); 
        header("Content-Transfer-Encoding: binary"); 
        header("Content-Length: ".filesize($row['FileName'])); 

        @readfile($row['FileName']); 
        exit(0); 
    }else{ 
        header("Location: /"); 
        exit; 
    }

六、用Email显示用户的Gravator头像

    $gravatar_link = 'http://www.gravatar.com/avatar/' . md5($comment_author_email) . '?s=32'; 
    echo '<img src="' . $gravatar_link . '" />';

七、用cURL获取RSS订阅数

    $ch = curl_init(); 
    curl_setopt($ch,CURLOPT_URL,'https://feedburner.google.com/api/awareness/1.0/GetFeedData?id=7qkrmib4r9rscbplq5qgadiiq4'); 
    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); 

八、时间差异计算

    /**
     * 时间差
     * @param  int $time 时间戳
     * @return string 
     */
    function ago($time)
    { 
       $periods = array("秒", "分", "小时", "天", "星期", "个月", "年", "十年"); 
        $lengths = array("60","60","24","7","4.35","12","10"); 

        $now = time(); 

        $difference = $now - $time; 
        $tense = "ago"; 

        for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) { 
            $difference /= $lengths[$j]; 
        } 

        $difference = round($difference); 

        return $difference .$periods[$j].'前'; 
    }

九、截取图片

    $filename= "test.jpg"; 
    list($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 = 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);

十、检查网站是否宕机

    function Visit($url){
        $agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows 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); 
        $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
        curl_close($ch); 
        if($httpcode>=200 && $httpcode<300) return true; 
        else return false; 
    } 
    if (Visit("http://www.hoehub.com")) 
        echo '服务器没有宕机!'; 
    else 
        echo '服务器宕机了!'; 
后端php

我来吐槽

*

*