PHP Common Code

Source: Internet
Author: User
One is the POST request function of curl, is mainly used for the development of Platform API, the processing function of the request interface, if you have multiple platforms, to pass data between each other, use this function absolutely useful:

PHP Code copy Content to clipboard

    1. /**
    2. * Curl Access Program Interface
    3. * @param string
    4. * @return Array
    5. */
    6. function Getcurldate ($url, $datas, $key) {
    7. $datas [' time '] = $_server[' request_time ' + 300;
    8. $post _data[' post '] = UrlEncode (Authcode (Serialize ($datas), "ENCODE", $key));
    9. echo $url;
    10. $ch = Curl_init ();
    11. curl_setopt ($ch, Curlopt_url, $url);
    12. curl_setopt ($ch, Curlopt_returntransfer, 1);
    13. We're in post data Oh!
    14. curl_setopt ($ch, Curlopt_post, 1);
    15. Add the post variable to the
    16. curl_setopt ($ch, Curlopt_postfields, $post _data);
    17. $output = curl_exec ($ch);
    18. Dump (Curl_error ($ch));
    19. Curl_close ($ch);
    20. Return Json_decode ($output, true);
    21. }

PHP Gets the file extension:

C + + Code copy content to clipboard

    1. /**
    2. * @ Get file name extension
    3. * @ $pic String picture path
    4. */
    5. function Get_file_ext ($pic) {
    6. Return substr ($pic, Strrpos ($pic, '. ') + 1);
    7. }

There is also a reversible encryption, decryption function (encryption of the same string will be encrypted into a different string, very useful)

PHP Code copy Content to clipboard

  1. /**
  2. * String Encryption
  3. * @param $string characters that need to be encrypted
  4. * @param $operation encryption or decryption
  5. * @param $key website encryption key, prevent crack
  6. * @return String
  7. */
  8. function Authcode ($string, $operation = ' DECODE ', $key = ", $expiry = 0) {
  9. $ckey _length = 4;
  10. $key = MD5 ($key? $key: ' ^www.itokit.com$ ');
  11. $keya = MD5 (substr ($key, 0, 16));
  12. $KEYB = MD5 (substr ($key, 16, 16));
  13. $KEYC = $ckey _length? ($operation = = ' DECODE '? substr ($string, 0, $ckey _length): substr (MD5 (Microtime ()),-$ckey _length)): ";
  14. $cryptkey = $keya. MD5 ($keya. $KEYC);
  15. $key _length = strlen ($cryptkey);
  16. $string = $operation = = = ' DECODE '? Base64_decode (substr ($string, $ckey _length)): sprintf ('%010d ', $expiry? $expiry + Time (): 0). SUBSTR (MD5 ($string. $keyb), 0, 16). $string;
  17. $string _length = strlen ($string);
  18. $result = ";
  19. $box = Range (0, 255);
  20. $rndkey = Array ();
  21. for ($i = 0; $i <= 255; $i + +) {
  22. $rndkey [$i] = Ord ($cryptkey [$i% $key _length]);
  23. }
  24. for ($j = $i = 0; $i < $i + +) {
  25. $j = ($j + $box [$i] + $rndkey [$i])% 256;
  26. $tmp = $box [$i];
  27. $box [$i] = $box [$j];
  28. $box [$j] = $tmp;
  29. }
  30. for ($a = $j = $i = 0; $i < $string _length; $i + +) {
  31. $a = ($a + 1)% 256;
  32. $j = ($j + $box [$a])% 256;
  33. $tmp = $box [$a];
  34. $box [$a] = $box [$j];
  35. $box [$j] = $tmp;
  36. $result. = Chr (ord ($string [$i]) ^ ($box [($box [$a] + $box [$j])% 256]));
  37. }
  38. if ($operation = = ' DECODE ') {
  39. if (substr ($result, 0, 0) = = 0 | | substr ($result,, Ten)-time () > 0) && substr ($result, ten, +) = = substr (M D5 (substr ($result,). $keyb), 0, 16) {
  40. Return substr ($result, 26);
  41. } else {
  42. Return ';
  43. }
  44. } else {
  45. Return $KEYC. Str_replace (' = ', ' ', Base64_encode ($result));
  46. }
  47. }

PHP Code copy Content to clipboard

    1. /**
    2. * String goto Hex
    3. * @param Unknown_type $s
    4. */
    5. function Str2hex ($s) {
    6. $r = "";
    7. $hexes = Array ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "B", "C", "D", "E", "F");
    8. for ($i =0; $i < li="">
    9. $r. = ($hexes [(Ord ($s {$i}) >> 4)]. $hexes [(Ord ($s {$i}) & 0xf)]);
    10. return $r;
    11. }

PHP Code copy Content to clipboard

    1. /**
    2. * Hexadecimal to String
    3. * @param Unknown_type $s
    4. */
    5. function Hex2str ($s) {
    6. $r = "";
    7. for ($i = 0; $i < li="">
    8. {
    9. $x 1 = Ord ($s {$i});
    10. $x 1 = ($x 1>=48 && $x 1<58)? $x 1-48: $x 1-97+10;
    11. $x 2 = Ord ($s {$i +1});
    12. $x 2 = ($x 2>=48 && $x 2<58)? $x 2-48: $x 2-97+10;
    13. $r. = Chr ((($x 1 << 4) & 0xf0) | ($x 2 & 0x0f));
    14. }
    15. return $r;
    16. }

PHP Code copy Content to clipboard

    1. /**
    2. * Returns a string or array that has been processed by addslashes
    3. * @param $string string or array to be processed
    4. * @return Mixed
    5. */
    6. function New_addslashes ($string) {
    7. if (!is_array ($string)) return addslashes ($string);
    8. foreach ($string as $key = + $val) $string [$key] = new_addslashes ($val);
    9. return $string;
    10. }
    11. /**/
    12. function Addslashes_deep ($string)
    13. {
    14. Return Is_array ($string)? Array_map (' Addslashes_deep ', $string): Addslashes ($string);
    15. }

PHP Code copy Content to clipboard

    1. /**
    2. * Returns a string or array that has been processed by stripslashes
    3. * @param $string string or array to be processed
    4. * @return Mixed
    5. */
    6. function New_stripslashes ($string) {
    7. if (!is_array ($string)) return stripslashes ($string);
    8. foreach ($string as $key = + $val) $string [$key] = new_stripslashes ($val);
    9. return $string;
    10. }
    11. /**/
    12. function Stripslashes_deep ($string)
    13. {
    14. Return Is_array ($string)? Array_map (' Stripslashes_deep ', $string): Stripslashes ($string);
    15. }

PHP Code copy Content to clipboard

    1. /**
    2. * Returns a string or array that has been processed by Htmlspecialchars
    3. * @param $string string or array to be processed
    4. * @return Mixed
    5. */
    6. function New_html_special_chars ($string) {
    7. if (!is_array ($string)) return Htmlspecialchars ($string);
    8. foreach ($string as $key = + $val) $string [$key] = New_html_special_chars ($val);
    9. return $string;
    10. }

PHP Code copy Content to clipboard

    1. /**
    2. * GET request IP
    3. *
    4. * @return IP Address
    5. */
    6. function IP () {
    7. if (getenv (' http_client_ip ') && strcasecmp (getenv (' http_client_ip '), ' unknown ') {
    8. $ip = getenv (' http_client_ip ');
    9. } elseif (getenv (' http_x_forwarded_for ') && strcasecmp (getenv (' http_x_forwarded_for '), ' unknown ') {
    10. $ip = getenv (' http_x_forwarded_for ');
    11. } elseif (getenv (' remote_addr ') && strcasecmp (getenv (' remote_addr '), ' unknown ') {
    12. $ip = getenv (' remote_addr ');
    13. } elseif (Isset ($_server[' remote_addr ") && $_server[' remote_addr '] && strcasecmp ($_server[' Remote_ ADDR '], ' unknown ') {
    14. $ip = $_server[' remote_addr ');
    15. }
    16. Return Preg_match ('/[\d\.] {7,15}/', $ip, $matches)? $matches [0]: ";
    17. }

PHP Code copy Content to clipboard

  1. /**
  2. * Character interception support UTF8/GBK
  3. * @param $string
  4. * @param $length
  5. * @param $dot
  6. */
  7. function Str_cut ($string, $length, $dot = ' ... ') {
  8. $strlen = strlen ($string);
  9. if ($strlen <= $length) return $string;
  10. $string = Str_replace (', ', ' & ', ' ' ', ' ' ', ' ' ', ' ' ' ', ' ' ', ' ' < ', ' > ', ' * ', ' ... '), Array (' ∵ ', ', ' & ', ' ' ', ' ' ' ', ' ' ', ' ' ', '? ', ' < ', ' > ', ' ' ', ' ' ... '), $string);
  11. $strcut = ";
  12. if (Strtolower (CHARSET) = = ' Utf-8 ') {
  13. $length = Intval ($length-strlen ($DOT)-$length/3);
  14. $n = $tn = $noc = 0;
  15. while ($n < strlen ($string)) {
  16. $t = Ord ($string [$n]);
  17. if ($t = = 9 | | $t = = 10 | | (<= $t && $t <= 126)) {
  18. $tn = 1; $n + +; $noc + +;
  19. } elseif (194 <= $t && $t <= 223) {
  20. $tn = 2; $n + = 2; $noc + = 2;
  21. } elseif (224 <= $t && $t <= 239) {
  22. $tn = 3; $n + = 3; $noc + = 2;
  23. } elseif (<= $t && $t <= 247) {
  24. $tn = 4; $n + = 4; $noc + = 2;
  25. } elseif (248 <= $t && $t <= 251) {
  26. $tn = 5; $n + = 5; $noc + = 2;
  27. } elseif ($t = = 252 | | $t = = 253) {
  28. $tn = 6; $n + = 6; $noc + = 2;
  29. } else {
  30. $n + +;
  31. }
  32. if ($noc >= $length) {
  33. Break
  34. }
  35. }
  36. if ($noc > $length) {
  37. $n-= $tn;
  38. }
  39. $strcut = substr ($string, 0, $n);
  40. $strcut = Str_replace (' ∵ ', ' & ', ' ' ', ' ' ', ' ' ', ' ' ', ' ' ', ' < ', ' > ', ' * ', ' ... '), Array (', ' & ', ' "', ') , ' ' ', ' ' ' ', ' '? ', ' < ', ' > ', ' ' ', ' ... '), $strcut);
  41. } else {
  42. $dotlen = strlen ($dot);
  43. $maxi = $length-$dotlen-1;
  44. $current _str = ";
  45. $search _arr = Array (' & ', ' ', ' ' ' ', ' ' ', ' ' ', ' ' ', ' ' ' ' ' ' ' ' ', ' ' ', ' ', ' ' < ', ' > ', ' * ', ' ... ', ' ∵ ');
  46. $replace _arr = Array (' & ', ' ', ' ' ' ', ' ' ', ' ' ', ' ' ', ' ' ' ' ' ' ' ' ' ' ', ' ' ', ' ' < ', ' > ', ' * ', ' ... ', ');
  47. $search _flip = Array_flip ($search _arr);
  48. for ($i = 0; $i < $maxi; $i + +) {
  49. $current _str = Ord ($string [$i]) > 127? $string [$i]. $string [+ + $i]: $string [$i];
  50. if (In_array ($current _str, $search _arr)) {
  51. $key = $search _flip[$current _str];
  52. $current _str = str_replace ($search _arr[$key], $replace _arr[$key], $current _str);
  53. }
  54. $strcut. = $current _str;
  55. }
  56. }
  57. return $strcut. $dot;
  58. }

PHP Code copy Content to clipboard

    1. /**
    2. * Generate random string
    3. *
    4. * @param int $length output length
    5. * @param string $chars Optional, default is 0123456789
    6. * @return String string
    7. */
    8. function random ($length, $chars = ' 0123456789 ') {
    9. $hash = ";
    10. $max = strlen ($chars)-1;
    11. for ($i = 0; $i < $length; $i + +) {
    12. $hash. = $chars [Mt_rand (0, $max)];
    13. }
    14. return $hash;
    15. }

PHP Code copy Content to clipboard

    1. /**
    2. * Convert string to array
    3. *
    4. * @param string $data strings
    5. * @return Array format is returned, if data is empty, an empty array is returned
    6. */
    7. function String2array ($data) {
    8. if ($data = = ") return array ();
    9. Eval ("\ $array = $data;");
    10. return $array;
    11. }

PHP Code copy Content to clipboard

    1. /**
    2. * Convert an array to a string
    3. *
    4. * @param array $data arrays
    5. * @param bool $isformdata If 0, do not use new_stripslashes processing, optional parameter, default is 1
    6. * @return string is returned, if data is empty, NULL is returned
    7. */
    8. function array2string ($data, $isformdata = 1) {
    9. if ($data = = ") return";
    10. if ($isformdata) $data = New_stripslashes ($data);
    11. Return Addslashes (Var_export ($data, TRUE));
    12. }

PHP Code copy Content to clipboard

    1. /**
    2. * Convert bytes to other units
    3. *
    4. *
    5. * @param string $filesize byte size
    6. * @return String return size
    7. */
    8. function Sizecount ($filesize) {
    9. if ($filesize >= 1073741824) {
    10. $filesize = Round ($filesize/1073741824 * 100)/100. ' GB ';
    11. } elseif ($filesize >= 1048576) {
    12. $filesize = Round ($filesize/1048576 * 100)/100. ' MB ';
    13. } elseif ($filesize >= 1024) {
    14. $filesize = Round ($filesize/1024 * 100)/100. ' KB ';
    15. } else {
    16. $filesize = $filesize. ' Bytes ';
    17. }
    18. return $filesize;
    19. }
  • 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.