
The examples in this article summarize some functions commonly used in PHP application development. These functions include character operations, file operations and other operations. They are shared with everyone. refer to. The details are as follows:
1. PHP encryption and decryption
PHP encryption and decryption functions can be used to encrypt some useful strings and store them in the database, and to reversibly decrypt the strings. This function uses base64 and MD5 encrypt and decrypt.
The code is as follows
1 2 3 4 5 6 7 8 9 | 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 ;
}
}
|
Copy after login
The usage method is as follows:
1 2 3 4 5 | echo encryptDecrypt('password', 'Helloweba欢迎您',0);
echo encryptDecrypt('password', 'z0JAx4qMwcF+db5TNbp/xwdUM84snRsXvvpXuaCa4Bk=',1);
|
Copy after login
2. PHP generates a random string
When we need to generate a random name, temporary password and other strings, we can use the following function:
The code is as follows
1 2 3 4 5 6 7 8 | function generateRandomString( $length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ( $i = 0; $i < $length ; $i ++) {
$randomString .= $characters [rand(0, strlen ( $characters ) - 1)];
}
return $randomString ;
}
|
Copy after login
The usage method is as follows:
1 | echo generateRandomString(20);
|
Copy after login
3. PHP obtains the file extension (suffix)
The following function can quickly obtain the file extension or suffix.
The code is as follows
1 2 3 4 | function getExtension( $filename ){
$myext = substr ( $filename , strrpos ( $filename , '.'));
return str_replace ('.','', $myext );
}
|
Copy after login
The usage method is as follows:
1 2 | $filename = '我的文档.doc';
echo getExtension( $filename );
|
Copy after login
4. PHP gets the file size and formats it
The functions used below can obtain the size of the file and convert it into KB, MB and other formats that are easy to read.
The code is as follows
1 2 3 4 5 6 7 8 | 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(1024, ( $i = floor (log( $size , 1024)))), 2) . $sizes [ $i ]);
}
}
|
Copy after login
The usage method is as follows:
1 2 | $thefile = filesize ('test_file.mp3');
echo formatSize( $thefile );
|
Copy after login
5. PHP replaces tag characters
Sometimes we need to replace strings and template tags with specified content. We can use the following function:
The code is as follows
1 2 3 4 | function stringParser( $string , $replacer ){
$result = str_replace ( array_keys ( $replacer ), array_values ( $replacer ), $string );
return $result ;
}
|
Copy after login
The usage method is as follows:
1 2 3 4 5 | $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}' => '','{br}' => '
');
echo stringParser( $string , $replace_array );
|
Copy after login
For more PHP related knowledge, please visit PHP Chinese website!
The above is the detailed content of A very practical summary of common PHP functions - encryption and decryption, string operations, file operations, SQL injection and other functions. For more information, please follow other related articles on the PHP Chinese website!