直接可以拿來用的PHP慣用功能程式碼片段(1~5)

來源:互聯網
上載者:User
直接可以拿來用的PHP常用功能程式碼片段(1~5)

文章來源:jquery教程?-?http://www.jq-school.com/Show.aspx?id=322

前面已經分享了100個常用的原生JavaScript程式碼片段,現在有空就開始收集PHP實用功能程式碼片段分享給大家用,希望可以幫到jquery學堂群裡面的成員和廣大對PHP開發的網友們提高開發效率,由於代碼比較長,每收集5個常用的程式碼片段就整理成一篇文章分享出來,以下是第一篇文章。

1、PHP實現即時網站

轉一個通過PHP實現即時網站功能

通過一個表單來提交請求處理頁面..點擊圖片下載!

';} else {header("Content-type: $imt");header("Content-Disposition: attachment; filename= $ifn");readfile($surl);}?>


2、PHP實現漢字轉整型數字,如:一百零一 轉成101

test();/** * 測試 */function test() {    echo CnToInt('一'); // 1    echo CnToInt('十'); // 10    echo CnToInt('十一'); // 11    echo CnToInt('一百一十'); // 110    echo CnToInt('一千零一'); // 1001    echo CnToInt('一萬零一百零一'); // 10101    echo CnToInt('一億一千三百萬零三千零一'); // 113003001    echo CnToInt('一千萬億'); // 11.0E+15}/** * 中文轉數字 * @param String $var 需要解析的中文數 * @param Int $start 初始值 * @return int */function CnToInt($var, $start = 0) {    if (is_numeric($var)) {        return $var;    }    if (intval($var) === 0) {        $splits = array('億' => 100000000, '萬' => 10000);        $chars = array('萬' => 10000, '千' => 1000, '百' => 100, '十' => 10, '一' => 1, '零' => 0);        $Ints = array('零' => 0, '一' => 1, '二' => 2, '三' => 3, '四' => 4, '五' => 5, '六' => 6, '七' => 7, '八' => 8, '九' => 9, '十' => 10);        $var = str_replace('零', "", $var);        foreach ($splits as $key => $step) {            if (strpos($var, $key)) {                $strs = explode($key, $var);                $start += CnToInt(array_shift($strs)) * $step;                $var = join('', $strs);            }        }        foreach ($chars as $key => $step) {            if (strpos($var, $key) !== FALSE) {                $vs = explode($key, $var);                if ($vs[0] === "") {                    $vs[0] = '一';                }                $start += $Ints[array_shift($vs)] * $step;                $var = join('', $vs);            } elseif (mb_strlen($var, 'utf-8') === 1) {                $start += $Ints[$var];                $var = '';                break;            }        }        return $start;    } else {        return intval($var);    }}


3、PHP實現簡單的對稱式加密和解密方法

/** * 通用加密 * @param String $string 需要加密的字串 * @param String $skey 加密EKY * @return String */function enCode($string = '', $skey = 'echounion') {    $skey = array_reverse(str_split($skey));    $strArr = str_split(base64_encode($string));    $strCount = count($strArr);    foreach ($skey as $key => $value) {        $key < $strCount && $strArr[$key].=$value;    }    return str_replace('=', 'O0O0O', join('', $strArr));}/** * 通用解密 * @param String $string 需要解密的字串 * @param String $skey 解密KEY * @return String */function deCode($string = '', $skey = 'echounion') {    $skey = array_reverse(str_split($skey));    $strArr = str_split(str_replace('O0O0O', '=', $string), 2);    $strCount = count($strArr);    foreach ($skey as $key => $value) {        $key < $strCount && $strArr[$key] = rtrim($strArr[$key], $value);    }    return base64_decode(join('', $strArr));}


4、PHP實現數字轉字母,產生Excel列標

/** * 數字轉字母 (類似於Excel列標) * @param Int $index 索引值 * @param Int $start 字母起始值 * @return String 返回字母 */function IntToChr($index, $start = 65) {$str = '';if (floor($index / 26) > 0) {$str .= IntToChr(floor($index / 26)-1);}return $str . chr($index % 26 + $start);}/** * 測試 */function test() {echo IntToChr(0); //# Aecho IntToChr(1); //# B// ...echo IntToChr(27); //# AB}


5、PHP實現計算兩個時間戳記之間的時間長度

/** * 返回兩個時間的相距時間,*年*月*日*時*分*秒 * @param int $one_time 時間一 * @param int $two_time 時間二 * @param int $return_type 預設值為0,0/不為0則拼接返回,1/*秒,2/*分*秒,3/*時*分*秒/,4/*日*時*分*秒,5/*月*日*時*分*秒,6/*年*月*日*時*分*秒 * @param array $format_array 格式化字元,例,array('年', '月', '日', '時', '分', '秒') * @return String or false */public function getRemainderTime($one_time, $two_time, $return_type=0, $format_array=array('年', '月', '日', '時', '分', '秒')){if($return_type<0 || $return_type>6){return false;}if(!(is_int($one_time) && is_int($two_time))){return false;}$remainder_seconds = abs($one_time-$two_time);//年$years = 0;if(($return_type==0 || $return_type==6) && $remainder_seconds-31536000>0){$years = floor($remainder_seconds/(31536000));}//月$monthes = 0;if(($return_type==0 || $return_type>=5) && $remainder_seconds-$years*31536000-2592000>0){$monthes = floor(($remainder_seconds-$years*31536000)/(2592000));}//日$days = 0;if(($return_type==0 || $return_type>=4) && $remainder_seconds-$years*31536000-$monthes*2592000-86400>0){$days = floor(($remainder_seconds-$years*31536000-$monthes*2592000)/(86400));}//時$hours = 0;if(($return_type==0 || $return_type>=3) && $remainder_seconds-$years*31536000-$monthes*2592000-$days*86400-3600>0){$hours = floor(($remainder_seconds-$years*31536000-$monthes*2592000-$days*86400)/3600);}//分$minutes = 0;if(($return_type==0 || $return_type>=2) && $remainder_seconds-$years*31536000-$monthes*2592000-$days*86400-$hours*3600-60>0){$minutes = floor(($remainder_seconds-$years*31536000-$monthes*2592000-$days*86400-$hours*3600)/60);}//秒$seconds = $remainder_seconds-$years*31536000-$monthes*2592000-$days*86400-$hours*3600-$minutes*60;$return = false;switch ($return_type){case 0:if($years>0){$return = $years.$format_array[0].$monthes.$format_array[1].$days.$format_array[2].$hours.$format_array[3].$minutes.$format_array[4].$seconds.$format_array[5];}else if($monthes>0){$return = $monthes.$format_array[1].$days.$format_array[2].$hours.$format_array[3].$minutes.$format_array[4].$seconds.$format_array[5];}else if($days>0){$return = $days.$format_array[2].$hours.$format_array[3].$minutes.$format_array[4].$seconds.$format_array[5];}else if($hours>0){$return = $hours.$format_array[3].$minutes.$format_array[4].$seconds.$format_array[5];}else if($minutes>0){$return = $minutes.$format_array[4].$seconds.$format_array[5];}else{$return = $seconds.$format_array[5];}break;            case 1:$return = $seconds.$format_array[5];break;case 2:$return = $minutes.$format_array[4].$seconds.$format_array[5];break;case 3:$return = $hours.$format_array[3].$minutes.$format_array[4].$seconds.$format_array[5];break;case 4:$return = $days.$format_array[2].$hours.$format_array[3].$minutes.$format_array[4].$seconds.$format_array[5];break;case 5:$return = $monthes.$format_array[1].$days.$format_array[2].$hours.$format_array[3].$minutes.$format_array[4].$seconds.$format_array[5];break;case 6:$return = $years.$format_array[0].$monthes.$format_array[1].$days.$format_array[2].$hours.$format_array[3].$minutes.$format_array[4].$seconds.$format_array[5];break;default:$return = false;                }return $return;}

?

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.