Common PHP function code snippets that can be used directly (1 ~ 5)

Source: Internet
Author: User
Tags difference between two times integer numbers
Common PHP function code snippets that can be used directly (1 ~ 5) Source: jquery tutorial? -? Http://www.jq-school.com/Show.aspx? Id = 322 I have previously shared 100 common native JavaScript code snippets. now I am free to start collecting common PHP functional code snippets that P can use directly (1 ~ 5)

Source: jquery tutorial? -? Http://www.jq-school.com/Show.aspx? Id = 322

I have already shared it.100 common native JavaScript code snippetsNow, I am free to collect PHP functional code snippets and share them with you.JqueryMembers of the school group andPHPDeveloped netizens improve the development efficiency. due to the long code, every 5 commonly used code snippets are collected into an article for sharing. The following is the first article.

1. PHP implements real-time website

Turn to PHP to implement real-time website functions

Submit the request processing page using a form ..
 Click to download the image!

';} Else {header ("Content-type: $ fe-"); header (" Content-Disposition: attachment; filename = $ ifen "); readfile ($ surl) ;}?>


2. PHP converts Chinese characters to integer numbers, for example, one hundred and one to 101

Test ();/*** test */function test () {echo CnToInt ('1'); // 1 echo CnToInt ('10 '); // 10 echo CnToInt ('11'); // 11 echo CnToInt ('20140901'); // One hundred and ten echo CnToInt ('20160901 '); // 1001 echo CnToInt ('20140901'); // ten thousand one hundred and one echo CnToInt ('20140901'); // 10101 echo CnToInt ('20160901 '); // 11.0E + 15}/*** convert Chinese to NUMBERS * @ param String $ var the number of Chinese characters to be parsed * @ param Int $ start initial value * @ return int */function CnToInt ($ var, $ start = 0) {I F (is_numeric ($ var) {return $ var;} if (intval ($ var) === 0) {$ splits = array (' '=> 100000000, 'wan' => 10000); $ chars = array ('wan' => 10000, 'kilobytes => 1000, 'bai' => 100, '10' => 10, '1' => 1, '0' => 0); $ Ints = array ('0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9, '10' => 10 ); $ var = str_replace ('0', "", $ 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] = '1';} $ 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 implements simple symmetric encryption and decryption methods

/*** Common encryption ** @ param String $ string the String to be encrypted * @ param String $ skey encrypt 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 ));} /*** common decryption ** @ param String $ string the String to be decrypted * @ param String $ skey decryption KEY * @ return string */function deCode ($ String = '', $ skey = 'echounion ') {$ skey = array_reverse (str_split ($ skey); $ strArr = str_split (str_replace ('0o0o',' = ', $ 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 converts numbers to letters to generate Excel column labels

/*** Convert a number to a letter (similar to an Excel column) * @ param Int $ index value * @ param Int $ start value * @ return String returns the letter */function IntToChr ($ index, $ start = 65) {$ str = ''; if (floor ($ index/26)> 0) {$ str. = IntToChr (floor ($ index/26)-1);} return $ str. chr ($ index % 26 + $ start);}/*** test */function test () {echo IntToChr (0); // # Aecho IntToChr (1 ); // # B //... echo IntToChr (27); // # AB}


5. PHP calculates the length of time between two timestamps.

/*** Returns the time difference between two times, * minute * Second * @ param int $ one_time 1 * @ param int $ two_time 2 * @ param int $ return_type default value: 0, 0/not if the value is 0, splicing returns, 1/* second, 2/* minute * second, 3/* hour * minute * second/, 4/* Day * hour * minute * second, 5/* month * Day * hour * minute * second, 6/* YEAR * month * Day * hour * minute * Second * @ param array $ format_array formatted character, for example, array (year, month, day, hour, minute, second ') * @ return String or false */public function getRemainderTime ($ one_time, $ two_time, $ return_type = 0, $ format_array = array ('year', 'month', 'day ', 'Time ', 'Minute', 'second ') {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); // year $ years = 0; if ($ return_type = 0 | $ return_type = 6) & $ remainder_seconds-31536000> 0) {$ years = floor ($ remainder_seconds/(31536000 ));} // month $ monthes = 0; if ($ return_type = 0 | $ return_type> = 5) & $ remainder_seconds-$ years * 31536000-2592000> 0) {$ monthes = floor ($ remainder_seconds-$ years * 31536000)/(2592000);} // day $ 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);} // hour $ 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);} // minute $ 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 );} // second $ 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 ;}

?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.