A detailed _php tutorial on the more general PHP self-built functions in some PHP projects

Source: Internet
Author: User
Some of the following PHP functions are the most commonly used project development functions of our IT dynamics, which are also more common in more projects.
1. Processing function of the request interface
Copy CodeThe 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're in post data Oh!
curl_setopt ($ch, Curlopt_post, 1);
Add the 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 CodeThe code is 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 functions
Copy CodeThe code is as follows:
/**
* String Encryption
* @param $string characters that need to be encrypted
* @param $operation encryption or decryption
* @param $key website encryption key, prevent crack
* @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 < $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) = = 0 | | substr ($result,, Ten)-time () > 0) && substr ($result, ten, +) = = substr (M D5 (substr ($result,). $keyb), 0, 16) {
Return substr ($result, 26);
} else {
Return ';
}
} else {
Return $KEYC. Str_replace (' = ', ' ', Base64_encode ($result));
}
}

4. String to Hex
Copy CodeThe code is as follows:
/**
* String goto Hex
* @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.16 Binary transcoding string
Copy CodeThe code is as follows:
/**
* Hexadecimal to String
* @param Unknown_type $s
*/
function Hex2str ($s) {
$r = "";
for ($i = 0; $i {
$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. Returning a string or array that has been processed by addslashes
Copy CodeThe code is as follows:
/**
* Returns a string or array that has been processed by addslashes
* @param $string 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. Returning a string or array that has been processed by stripslashes
Copy CodeThe code is as follows:
/**
* Returns a string or array that has been processed by stripslashes
* @param $string 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. Returning a string or array that has been processed by Htmlspecialchars
Copy CodeThe code is as follows:
/**
* Returns a string or array that has been processed by Htmlspecialchars
* @param $string 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 CodeThe code is 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 CodeThe code is 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 (' ∵ ', ', ' & ', ' ' ', ' ' ', ' ' ', ' ' ', '-', ' < ', ' > ', ' ' ', ' ' ... '), $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 (', ' & ', ' "', ') , ' ' ', ' ' ' ', '-', ' < ', ' > ', ' ' ', ' ... '), $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. Generating Random strings
Copy CodeThe code is as follows:
/**
* Generate random string
*
* @param int $length output length
* @param string $chars Optional, default is 0123456789
* @return String 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 Strings to arrays
Copy CodeThe code is as follows:
/**
* Convert string to array
*
* @param string $data strings
* @return Array format is returned, if data is empty, an empty array is returned
*/
function String2array ($data) {
if ($data = = ") return array ();
Eval ("\ $array = $data;");
return $array;
}

13. Converting an array to a string
Copy CodeThe code is as follows:
/**
* Convert an array to a string
*
* @param array $data arrays
* @param bool $isformdata If 0, do not use new_stripslashes processing, optional parameter, default is 1
* @return string is returned, if data is empty, NULL is returned
*/
function array2string ($data, $isformdata = 1) {
if ($data = = ") return";
if ($isformdata) $data = New_stripslashes ($data);
Return Addslashes (Var_export ($data, TRUE));
}

14. Convert the number of bytes to another unit
Copy CodeThe code is as follows:
/**
* Convert bytes 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;
}

http://www.bkjia.com/PHPjc/327409.html www.bkjia.com true http://www.bkjia.com/PHPjc/327409.html techarticle some of the following PHP functions are the most commonly used project development functions of our IT dynamics, which are also more common in more projects. 1. Processing function of the request interface ...

  • Related Article

    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.