This article provides a detailed analysis of common php self-built functions in some php projects. For more information, see
This article provides a detailed analysis of common php self-built functions in some php projects. For more information, see
The following php functions are the most commonly used project development functions of it Power. These functions are used in many projects and are also quite common.
1. processing functions of the Request Interface
The Code is as follows:
/**
* Curl Access Program Interface
* @ Param string
* @ Return array
*/
Function getCurlDate ($ url, $ datas, $ key ){
$ Datas ['time'] = $ _ SERVER ['request _ time'] + 300;
$ Post_data ['post'] = urlencode (authcode (serialize ($ datas), "ENCODE", $ key ));
// Echo $ url;
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
// We are posting data!
Curl_setopt ($ ch, CURLOPT_POST, 1 );
// Add the post variable
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post_data );
$ Output = curl_exec ($ ch );
// Dump (curl_error ($ ch ));
Curl_close ($ ch );
Return json_decode ($ output, true );
}
2. Get the file extension
The Code is as follows:
/**
* @ Get the file extension
* @ $ Pic string image path
*/
Function get_file_ext ($ pic ){
Return substr ($ pic, strrpos ($ pic, '.') + 1 );
}
3. reversible encryption and decryption functions
The Code is as follows:
/**
* String Encryption
* @ Param $ string the characters to be encrypted
* @ Param $ operation encryption or decryption
* @ Param $ key: website encryption key, Hong Kong Space, and U.S. space to Prevent Cracking
* @ Return string
*/
Function authcode ($ string, $ operation = 'decode', $ key = '', $ expiry = 0 ){
$ Ckey_length = 4;
$ Key = md5 ($ key? $ Key: '^ $ ');
$ Keya = md5 (substr ($ key, 0, 16 ));
$ Keyb = md5 (substr ($ key, 16, 16 ));
$ Keyc = $ ckey_length? ($ Operation = 'decode '? Substr ($ string, 0, $ ckey_length): substr (md5 (microtime (),-$ ckey_length )):'';
$ Cryptkey = $ keya. md5 ($ keya. $ keyc );
$ Key_length = strlen ($ cryptkey );
$ String = $ operation = 'decode '? Base64_decode (substr ($ string, $ ckey_length): sprintf ('% 010d', $ expiry? $ Expiry + time (): 0). substr (md5 ($ string. $ keyb), 0, 16). $ string;
$ String_length = strlen ($ string );
$ Result = '';
$ Box = range (1, 0,255 );
$ Rndkey = array ();
For ($ I = 0; $ I <= 255; $ I ++ ){
$ Rndkey [$ I] = ord ($ cryptkey [$ I % $ key_length]);
}
For ($ j = $ I = 0; I I <256; $ I ++ ){
$ J = ($ j + $ box [$ I] + $ rndkey [$ I]) % 256;
$ Tmp = $ box [$ I];
$ Box [$ I] = $ box [$ j];
$ Box [$ j] = $ tmp;
}
For ($ a = $ j = $ I = 0; $ I <$ string_length; $ I ++ ){
$ A = ($ a + 1) % 256;
$ J = ($ j + $ box [$ a]) % 256;
$ Tmp = $ box [$ a];
$ Box [$ a] = $ box [$ j];
$ Box [$ j] = $ tmp;
$ Result. = chr (ord ($ string [$ I]) ^ ($ box [($ box [$ a] + $ box [$ j]) % 256]);
}
If ($ operation = 'decode '){
If (substr ($ result, 0, 10) = 0 | substr ($ result, 0, 10)-time ()> 0) & substr ($ result, 10, 16) = substr (md5 (substr ($ result, 26 ). $ keyb), 0, 16 )){
Return substr ($ result, 26 );
} Else {
Return '';
}
} Else {
Return $ keyc. str_replace ('=', '', base64_encode ($ result ));
}
}
4. Convert string to hexadecimal
The Code is as follows:
/**
* Convert string to hexadecimal
* @ Param unknown_type $ s
*/
Function str2hex ($ s ){
$ R = "";
$ Hexes = array ("0", "1", "2", "3", "4", "5", "6", "7 ", "8", "9", "a", "B", "c", "d", "e", "f ");
For ($ I = 0; $ I $ R. = ($ hexes [(ord ($ s {$ I})> 4)]. $ hexes [(ord ($ s {$ I}) & 0xf)]);
Return $ r;
}
5. hexadecimal conversion string
The Code is as follows:
/**
* Hexadecimal conversion string
* @ Param unknown_type $ s
*/
Function hex2str ($ s ){
$ R = "";
For ($ I = 0; $ I {
$ X1 = ord ($ s {$ I });
$ X1 = ($ x1 >=48 & $ x1 <58 )? $ X1-48: $ x1-97 + 10;
$ X2 = ord ($ s {$ I + 1 });
$ X2 = ($ x2 >=48 & $ x2 <58 )? $ X2-48: $ x2-97 + 10;
$ R. = chr ($ x1 <4) & 0xf0) | ($ x2 & 0x0f ));
}
Return $ r;
}
6. Return the strings or arrays processed by addslashes.
The Code is as follows:
/**
* Returns a string or array processed by addslashes.
* @ Param $ string the string or array to be processed
* @ Return mixed
*/
Function new_addslashes ($ string ){
If (! Is_array ($ string) return addslashes ($ string );
Foreach ($ string as $ key => $ val) $ string [$ key] = new_addslashes ($ val );
Return $ string;
}
/**/
Function addslashes_deep ($ string)
{
Return is_array ($ string )? Array_map ('addslashes _ deep ', $ string): addslashes ($ string );
}
7. Return strings or arrays processed by stripslashes
The Code is as follows:
/**
* Returns the string or array processed by stripslashes.
* @ Param $ string the string or array to be processed
* @ Return mixed
*/
Function new_stripslashes ($ string ){
If (! Is_array ($ string) return stripslashes ($ string );
Foreach ($ string as $ key => $ val) $ string [$ key] = new_stripslashes ($ val );
Return $ string;
}
/**/
Function stripslashes_deep ($ string)
{
Return is_array ($ string )? Array_map ('stripslashes _ deep ', $ string): stripslashes ($ string );
}
8. Returns a string or array processed by htmlspecialchars.
The Code is as follows: