PHP idioms that can be used directly in the code snippet

Source: Internet
Author: User
Code Snippets for PHP functions that can be used directly

Article Source: jquery tutorial?-? http://www.jq-school.com/Show.aspx?id=322

We have already shared 100 common native JavaScript code snippets , and now we are free to start collecting PHP utility code snippets to share with you, hoping to help the members of the jquery Academy and the vast majority of PHP development of netizens to improve the development efficiency, because the code is relatively long, each collection of 5 commonly used code snippets to organize into an article to share, the following is the first article.

1, PHP implementation of the instant website

Turn to a real-time website via PHP

Submit the request processing page through a table forms:
 
   Click on the image to download!

';} else {header ("Content-type: $imt"); header ("Content-disposition:attachment; Filename= $IFN "); ReadFile ($surl);}? >


2, PHP to achieve the Chinese character to the integer number, such as: 101 Turn into 101

Test ();/** * Testing */function Test () {echo cntoint (' one ');//1 echo cntoint (' ten ');//Echo Cntoint (' 11 ');//11 Echo cntoint (' 110 '); echo cntoint (' 1001 '); 1001 Echo cntoint (' 10,101 '); 10101 echo cntoint (' 113,003,001 '); 113003001 echo cntoint (' 1000 trillion '); 11.0e+15}/** * Chinese to Digital * @param String $var The number of Chinese to parse * @param int $start initial value * @return int */function cntoint ($var, $star    t = 0) {if (Is_numeric ($var)) {return $var;        if (intval ($var) = = = 0) {$splits = array (' billion ' = 100000000, ' million ' = 10000);        $chars = Array (' million ' = 10000, ' thousand ' = 1000, ' hundred ' = 100, ' ten ' = 10, ' One ' + 1, ' 0 ' = 0); $Ints = Array (' 0 ' = + 0, ' one ' = 1, ' two ' = 2, ' three ' = ' 3, ' four ' = 4, ' Five ' = 5, ' six ' = ' 6 ', ' seven ' = ' 7 ', ' eight '        = 8, ' Nine ' = 9, ' Ten ' = 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] = ' one ';                } $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 implementation of a simple symmetric encryption and decryption method

/** * Universal Encryption * @param string $string strings that need to be encrypted * @param string $skey Encrypt Eky * @return string */function enCode ($string = ", $s Key = ' 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));} /** * Universal Decryption * @param string $string The string to decrypt * @param string $skey Decrypt key * @return string */function deCode ($string = ", $s Key = ' 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 to achieve the number of letters to create Excel column label

/** * Numeric to letter (similar to Excel column label) * @param int $index index value * @param int $start Letter Start value * @return String returns the letter */function INTTOCHR ($inde X, $start = n) {$str = ", if (floor ($index/26) > 0) {$str. = INTTOCHR (Floor ($index/26)-1);} Return $STR. Chr ($index% + $start);} /** * Testing */function Test () {echo inttochr (0)//# Aecho inttochr (1);//# b//... echo inttochr;//# AB}


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

/** * Returns two time apart time, * year * month * day * time * minute * seconds * @param int $one _time time one * @param int $two _time time two * @param int $return _type default value 0,0/not 0 Then stitching back, 1/* seconds, 2/* minutes * seconds, 3/* * minutes * seconds/,4/* days * minutes * seconds, 5/* months * days * minutes * seconds, 6/* year * month * day * minutes * seconds * @param array $format _array formatted character, example, Array (' Year ', '  Month ', ' Day ', ' time ', ' minutes ', ' seconds ') * @return String or false */public function Getremaindertime ($one _time, $two _time, $return _type=0, $format _array=array (' Year ', ' Month ', ' Day ', ' Time ', ' min ', ' sec ')) {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));} $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);} Sub $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 $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.