1, PHP encryption and decryption
function Encryptdecrypt ($key, $string, $decrypt) {
if ($decrypt) {
$decrypted = RTrim (Mcrypt_decrypt (mcrypt_rijndael_256, MD5 ($key), Base64_decode ($string), MCRYPT_MODE_CBC, MD5 (MD5 ($ Key)), "12");
return $decrypted;
}else{
$encrypted = Base64_encode (Mcrypt_encrypt (mcrypt_rijndael_256, MD5 ($key), $string, MCRYPT_MODE_CBC, MD5 (MD5 ($KEY)))) ;
return $encrypted;
}
}//encryption:
echo encryptdecrypt (' Password ', ' Helloweba Welcome ', 0);
Decrypt:
echo encryptdecrypt (' Password ', ' z0jax4qmwcf+db5tnbp/xwdum84snrsxvvpxuaca4bk= ', 1);
2. PHP generates random string
function generaterandomstring ($length = 10) {
$characters = ' 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ';
$randomString = ";
for ($i = 0; $i < $length; $i + +) {
$randomString. = $characters [rand (0, strlen ($characters)-1)];
}
return $randomString;
}
Echo generaterandomstring (20);
3. PHP Gets the file extension (suffix)
function GetExtension ($filename) {
$myext = substr ($filename, Strrpos ($filename, '. '));
Return Str_replace ('. ', ', $myext);
}
$filename = ' My Documents. Doc ';
echo getextension ($filename);
4, PHP get file size and format
function Formatsize ($size) {
$sizes = Array ("Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB");
if ($size = = 0) {
Return (' N/a ');
} else {
Return (Round ($size/pow ($i = Floor (log ($size, 1024x768))), 2). $sizes [$i]);
}
}
$thefile = filesize (' Test_file.mp3 ');
echo formatsize ($thefile);
5. PHP Replacement Label characters
function Stringparser ($string, $replacer) {
$result = Str_replace (Array_keys ($replacer), Array_values ($replacer), $string);
return $result;
}
$string = ' The {b}anchor text{/b} is the {b}actual word{/b} or words used {br}to describe the link {br}itself ';
$replace _array = Array (' {b} ' = ' <b> ', ' {/b} ' = ' </b> ', ' {br} ' = ' <br/> ');
Echo Stringparser ($string, $replace _array);
6.php Gets the current page URL
function Curpageurl () {
$pageURL = ' http ';
if (!empty ($_server[' HTTPS ')) {$pageURL. = "S";}
$pageURL. = "://";
if ($_server["Server_port"]! = "80") {
$pageURL. = $_server["SERVER_NAME"]. ":". $_server["Server_port"].$_server["Request_uri"];
} else {
$pageURL. = $_server["SERVER_NAME"].$_server["Request_uri"];
}
return $pageURL;
}
echo Curpageurl ();
7.php Force download file
function Download ($filename) {
if ((Isset ($filename)) && (File_exists ($filename))) {
Header ("Content-length:". FileSize ($filename));
Header (' Content-type:application/octet-stream ');
Header (' content-disposition:attachment; Filename= '. $filename. ‘"‘);
ReadFile ("$filename");
} else {
echo "Looks like file does not exist!";
}
}
download ('/down/test_45f73e852.zip ');
8,php intercept string length
/*
Chinese character interception function supported by Utf-8 and gb2312
Cut_str (string, intercept length, start length, encode);
encoding defaults to Utf-8
Start length defaults to 0
*/
function Cutstr ($string, $sublen, $start = 0, $code = ' UTF-8 ') {
if ($code = = ' UTF-8 ') {
$pa = "/[x01-x7f]| [XC2-XDF] [x80-xbf]|xe0[xa0-xbf][x80-xbf]| [Xe1-xef] [X80-XBF] [x80-xbf]|xf0[x90-xbf][x80-xbf][x80-xbf]| [Xf1-xf7] [X80-XBF] [X80-XBF] [x80-xbf]/];
Preg_match_all ($pa, $string, $t _string);
if (count ($t _string[0])-$start > $sublen) return join ("', Array_slice ($t _string[0], $start, $sublen))." ...";
return join ("', Array_slice ($t _string[0], $start, $sublen));
}else{
$start = $start;
$sublen = $sublen;
$strlen = strlen ($string);
$tmpstr = ";
for ($i =0; $i < $strlen; $i + +) {
if ($i >= $start && $i < ($start + $sublen)) {
if (Ord (substr ($string, $i, 1)) >129) {
$tmpstr. = substr ($string, $i, 2);
}else{
$tmpstr. = substr ($string, $i, 1);
}
}
if (Ord (substr ($string, $i, 1)) >129) $i + +;
}
if (strlen ($TMPSTR) < $strlen) $tmpstr. = "...";
return $tmpstr;
}
}
$str = "Load images and page effects implemented by jquery plug-ins";
Echo Cutstr ($STR, 16);
9.
PHP Common Code Snippet: