Common PHP code

Source: Internet
Author: User
One common PHP code is the post request function of curl. it is mainly used for api development on various platforms to process request interfaces. if you have multiple platforms, data must be transmitted between them, this function is absolutely useful:

Copy content from PHP Code 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 are posting data!
  14. Curl_setopt ($ ch, CURLOPT_POST, 1 );
  15. // Add the post variable
  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 obtains the file extension:

C/C ++ Code: copy the content to the clipboard.

  1. /**
  2. * @ Get the file extension
  3. * @ $ Pic string Image path
  4. */
  5. Function get_file_ext ($ pic ){
  6. Return substr ($ pic, strrpos ($ pic, '.') + 1 );
  7. }

There is also a reversible encryption and decryption function (the same string will be encrypted into different strings for good use)

Copy content from PHP Code to clipboard

  1. /**
  2. * String encryption
  3. * @ Param $ string the characters to be encrypted
  4. * @ Param $ operation encryption or decryption
  5. * @ Param $ key: website encryption key to prevent cracking
  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 (1, 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 <256; $ 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, 10) = 0 | substr ($ result, 0, 10)-time ()> 0) & substr ($ result, 10, 16) = substr (md5 (substr ($ result, 26 ). $ 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. }

Copy content from PHP Code to clipboard

  1. /**
  2. * Convert string to hexadecimal
  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
  9. $ R. = ($ hexes [(ord ($ s {$ I})> 4)]. $ hexes [(ord ($ s {$ I}) & 0xf)]);
  10. Return $ r;
  11. }

Copy content from PHP Code to clipboard

  1. /**
  2. * Hexadecimal conversion string
  3. * @ Param unknown_type $ s
  4. */
  5. Function hex2str ($ s ){
  6. $ R = "";
  7. For ($ I = 0; $ I
  8. {
  9. $ X1 = ord ($ s {$ I });
  10. $ X1 = ($ x1 >=48 & $ x1 <58 )? $ X1-48: $ x1-97 + 10;
  11. $ X2 = ord ($ s {$ I + 1 });
  12. $ X2 = ($ x2 >=48 & $ x2 <58 )? $ X2-48: $ x2-97 + 10;
  13. $ R. = chr ($ x1 <4) & 0xf0) | ($ x2 & 0x0f ));
  14. }
  15. Return $ r;
  16. }

Copy content from PHP Code to clipboard

  1. /**
  2. * Returns a string or array processed by addslashes.
  3. * @ Param $ string the 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. }

Copy content from PHP Code to clipboard

  1. /**
  2. * Returns the string or array processed by stripslashes.
  3. * @ Param $ string the 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. }

Copy content from PHP Code to clipboard

  1. /**
  2. * Returns a string or array processed by htmlspecialchars.
  3. * @ Param $ string the 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. }

Copy content from PHP Code to clipboard

  1. /**
  2. * Obtain the request ip address
  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. }

Copy content from PHP Code to clipboard

  1. /**
  2. * UTF-8/GBK character truncation is supported.
  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 ('','', '&', '"', ''', '? ',' <','> ','·','... '), Array ('region', '',' & ','" ', "'", '",', '? ',' <','> ','·','... '), $ 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 | (32 <= $ 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 (240 <=$ 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 ('hour', '&', '"'," '",'? ',' <','> ','·','... '), Array ('',' & ','" ', ''','? ',' <','> ','·','... '), $ Strcut );
  41. } Else {
  42. $ Dotlen = strlen ($ dot );
  43. $ Maxi = $ length-$ dotlen-1;
  44. $ Current_str = '';
  45. $ Search_arr = array ('&', '', '"', "'",'? ',' <','> ','·','... ', 'Signature ');
  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. }

Copy content from PHP Code to clipboard

  1. /**
  2. * Generate random strings
  3. *
  4. * @ Param int $ length output length
  5. * @ Param string $ chars (optional). The default value is 0123456789.
  6. * @ Return string
  7. */
  8. Function random ($ length, $ chars = '20140901 '){
  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. }

Copy content from PHP Code to clipboard

  1. /**
  2. * Convert a string to an array
  3. *
  4. * @ Param string $ data string
  5. * @ Return array returns the array format. if data is null, an empty array is returned.
  6. */
  7. Function string2array ($ data ){
  8. If ($ data = '') return array ();
  9. Eval ("\ $ array = $ data ;");
  10. Return $ array;
  11. }

Copy content from PHP Code to clipboard

  1. /**
  2. * Convert an array to a string
  3. *
  4. * @ Param array $ data array
  5. * @ Param bool $ isformdata if it is 0, new_stripslashes is not used for processing. optional. the default value is 1.
  6. * @ Return string returns a string. if data is null, 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. }

Copy content from PHP Code to clipboard

  1. /**
  2. * The number of converted bytes is in 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.