Some PHP projects in the comparison of common PHP self-built functions of the detailed _php tips

Source: Internet
Author: User
Tags ord
Some of the following PHP functions are the most commonly used project development functions of our IT power, these functions are used in more projects, and are more general.
1. Processing function of Request interface
Copy Code code 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 in post data Oh!
curl_setopt ($ch, Curlopt_post, 1);
Add a post variable to the
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 name extension
Copy Code code as follows:

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

3. Reversible encryption and decryption function
Copy Code code as follows:

/**
* String Encryption
* @param $string characters that need to be encrypted
* @param $operation encryption or decryption
* @param $key website encryption key to prevent cracking
* @return String
*/
function Authcode ($string, $operation = ' DECODE ', $key = ', $expiry = 0) {
$ckey _length = 4;
$key = MD5 ($key $key: ' ^www.itokit.com$ ');
$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 (0, 255);

$rndkey = Array ();
for ($i = 0; $i <= 255; $i + +) {
$rndkey [$i] = Ord ($cryptkey [$i% $key _length]);
}

for ($j = $i = 0; $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,) = 0 | | substr ($result, 0)-time () > 0) && substr ($result,) = = substr (M D5 (substr ($result). $keyb), 0, 16)) {
Return substr ($result, 26);
} else {
Return ";
}
} else {
Return $KEYC. Str_replace (' = ', ', ', Base64_encode ($result));
}
}

4. String hex
Copy Code code as follows:

/**
* String 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 <strlen ($s); $i + +)
$r. = ($hexes [(Ord ($s {$i}) >> 4]]. $hexes [(Ord ($s {$i}) & 0xf]);
return $r;
}

5.16-in-turn string
Copy Code code as follows:

/**
* Hexadecimal spin string
* @param Unknown_type $s
*/
function Hex2str ($s) {
$r = "";
for ($i = 0; $i <strlen ($s); $i + + 2)
{
$x 1 = Ord ($s {$i});
$x 1 = ($x 1>=48 && $x 1<58)? $x 1-48: $x 1-97+10;
$x 2 = Ord ($s {$i +1});
$x 2 = ($x 2>=48 && $x 2<58)? $x 2-48: $x 2-97+10;
$r. = Chr (($x 1 << 4) & 0xf0 | ($x 2 & 0x0f));
}
return $r;
}

6. Returns a string or array that has been processed by addslashes
Copy Code code as follows:

/**
* Returns a string or array that has been processed by addslashes
* @param $string a 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. Returns a string or array that has been processed by stripslashes
Copy Code code as follows:

/**
* Returns a string or array that has been processed by stripslashes
* @param $string a 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 that has been processed by Htmlspecialchars
Copy Code code as follows:

/**
* Returns a string or array that has been processed by Htmlspecialchars
* @param $string a string or array to be processed
* @return Mixed
*/
function New_html_special_chars ($string) {
if (!is_array ($string)) return Htmlspecialchars ($string);
foreach ($string as $key => $val) $string [$key] = New_html_special_chars ($val);
return $string;
}

9. Get Request IP
Copy Code code as follows:

/**
* GET request IP
*
* @return IP Address
*/
function IP () {
if (getenv (' http_client_ip ') && strcasecmp (getenv (' http_client_ip '), ' unknown ')) {
$ip = getenv (' http_client_ip ');
} elseif (getenv (' http_x_forwarded_for ') && strcasecmp (getenv (' http_x_forwarded_for '), ' unknown ')) {
$ip = getenv (' http_x_forwarded_for ');
} elseif (getenv (' remote_addr ') && strcasecmp (getenv (' remote_addr '), ' unknown ')) {
$ip = getenv (' remote_addr ');
} elseif (Isset ($_server[' remote_addr ')) && $_server[' remote_addr '] && strcasecmp ($_server[' Remote_ ADDR '], ' unknown ') {
$ip = $_server[' remote_addr '];
}
Return Preg_match ('/[\d\.] {7,15}/', $ip, $matches)? $matches [0]: ';
}

10. Character Interception support UTF8/GBK
Copy Code code as follows:

/**
* Character interception support UTF8/GBK
* @param $string
* @param $length
* @param $dot
*/
function Str_cut ($string, $length, $dot = ' ... ') {
$strlen = strlen ($string);
if ($strlen <= $length) return $string;
$string = str_replace (Array (', ') ', ' & ', ' ', ', ', ' ', ' ', ' ', ' ', ' ', ', ', ' < ', ' > ', ', ', ' ... ', array (' ∵ ', ', ', ' & ', ', ' ', ', ' ', ' ', ' ', ', ', ' < ', ' > ', ' ', ' ... ', $string);
$strcut = ';
if (Strtolower (CHARSET) = = ' Utf-8 ') {
$length = Intval ($length-strlen ($DOT)-$length/3);
$n = $tn = $noc = 0;
while ($n < strlen ($string)) {
$t = Ord ($string [$n]);
if ($t = = 9 | | $t = 10 | | (<= $t && $t <= 126)) {
$tn = 1; $n + +; $noc + +;
} elseif (194 <= $t && $t <= 223) {
$tn = 2; $n + 2; $noc + 2;
} elseif (224 <= $t && $t <= 239) {
$tn = 3; $n + 3; $noc + 2;
ElseIf (<= $t && $t <= 247) {
$tn = 4; $n + 4; $noc + 2;
} elseif (248 <= $t && $t <= 251) {
$tn = 5; $n + 5; $noc + 2;
} elseif ($t = = 252 | | $t = = 253) {
$tn = 6; $n + 6; $noc + 2;
} else {
$n + +;
}
if ($noc >= $length) {
Break
}
}
if ($noc > $length) {
$n-= $tn;
}
$strcut = substr ($string, 0, $n);
$strcut = str_replace (Array (' ∵ ', ' & ', ', ', ', ', ', ', ', ', ', ', ', ', ', ' < ', ' > ', ', ', ' ... ', Array (', ' & ', ' "', ', ' , ' ', ' ', ' ', '-', ' < ', ' > ', ' • ', ' ... ', $strcut);
} else {
$dotlen = strlen ($dot);
$maxi = $length-$dotlen-1;
$current _str = ';
$search _arr = Array (' & ', ', ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ', ' < ', ' > ', ', ', ' ... ', ' ∵ ');
$replace _arr = Array (' & ', ', ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' < ', ' > ', ' ', ' ... ', ') ';
$search _flip = Array_flip ($search _arr);
for ($i = 0; $i < $maxi; $i + +) {
$current _str = Ord ($string [$i]) > 127? $string [$i]. $string [+ + $i]: $string [$i];
if (In_array ($current _str, $search _arr)) {
$key = $search _flip[$current _str];
$current _str = str_replace ($search _arr[$key], $replace _arr[$key], $current _str);
}
$strcut. = $current _str;
}
}
return $strcut. $dot;
}

11. Generate Random String
Copy Code code as follows:

/**
* Generate random string
*
* @param int $length output length
* @param string $chars Optional, defaults to 0123456789
* @return String
*/
function random ($length, $chars = ' 0123456789 ') {
$hash = ';
$max = strlen ($chars)-1;
for ($i = 0; $i < $length; $i + +) {
$hash. = $chars [Mt_rand (0, $max)];
}
return $hash;
}

12. Convert a string to an array
Copy Code code as follows:

/**
* Convert a string to an array
*
* @param string $data strings
* Returns the array format @return array, and returns an empty array if data is empty
*/
function String2array ($data) {
if ($data = = ") return array ();
Eval ("\ $array = $data;");
return $array;
}

13. Convert an array to a string
Copy Code code as follows:

/**
* Convert an array to a string
*
* Array $data @param array
* @param bool $isformdata If 0, do not use new_stripslashes processing, optional parameters, default is 1
* @return string, if NULL, returns null
*/
function array2string ($data, $isformdata = 1) {
if ($data = = ") return";
if ($isformdata) $data = New_stripslashes ($data);
Return Addslashes (Var_export ($data, TRUE));
}

14. Convert bytes to other units
Copy Code code as follows:

/**
* Convert byte number to other units
*
*
* @param string $filesize byte size
* @return String return size
*/
function Sizecount ($filesize) {
if ($filesize >= 1073741824) {
$filesize = Round ($filesize/1073741824 * 100)/100. ' GB ';
} elseif ($filesize >= 1048576) {
$filesize = Round ($filesize/1048576 * 100)/100. ' MB ';
} elseif ($filesize >= 1024) {
$filesize = Round ($filesize/1024 * 100)/100. ' KB ';
} else {
$filesize = $filesize. ' Bytes ';
}
return $filesize;
}

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.