1. PHP Encryption and decryption functions
PHP encryption and decryption functions can be used to encrypt some useful strings stored in the database, and by reversible decryption of the string, the function uses Base64 and MD5 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))); return $decrypted; } else{ $encrypted = Base64_encode (Mcrypt_encrypt (mcrypt_rijndael_256, MD5 ($key), $string, MCRYPT_MODE_CBC, MD5 ( MD5 ($key)))); return $encrypted; } }
Here's how to use it:
The following is the string "Helloweba welcome you" separately encrypted and decrypted//encrypted: Echo encryptdecrypt (' Password ', ' Helloweba Welcome ', 0); Decryption: Echo encryptdecrypt (' Password ', ' z0jax4qmwcf+db5tnbp/xwdum84snrsxvvpxuaca4bk= ', 1);
2. PHP generates random string
The following functions are used when we need to generate a random name, a temporary password, and so on:
function generaterandomstring ($length = ten) { $characters = ' 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ '; $randomString = "; for ($i = 0; $i < $length; $i + +) { $randomString. = $characters [rand (0, strlen ($characters)-1)]; } return $randomString; }
Here's how to use it:
Echo generaterandomstring (20);
3. PHP Gets the file extension (suffix)
The following functions can quickly get the extension of a file as a suffix.
function GetExtension ($filename) { $myext = substr ($filename, Strrpos ($filename, '. ')); Return Str_replace ('. ', ', $myext); }
Here's how to use it:
$filename = ' My Documents. Doc '; echo getextension ($filename);
4, PHP get file size and format
The following functions can be used to obtain the size of the file and convert it into a readable format such as KB,MB.
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, 1024))), 2). $sizes [$i]); } }
Here's how to use it: View Demo
$thefile = filesize (' Test_file.mp3 '); echo formatsize ($thefile);
5. PHP Replacement Label characters
Sometimes we need to replace the string and template tag with the specified content, and we can use the following function:
function Stringparser ($string, $replacer) { $result = Str_replace (Array_keys ($replacer), Array_values ($replacer) , $string); return $result; }
Here's how to use it:
$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 lists the file name under the directory
If you want to list all the files in the directory, use the following code:
function Listdirfiles ($DirPath) { if ($dir = Opendir ($DirPath)) {while (($file = Readdir ($dir))!== false) { if (!is_dir ($DirPath. $file)) { echo "filename: $file <br/>"; } } }
Here's how to use it:
Listdirfiles (' home/some_folder/');