This article mainly introduces common PHP functions and summarizes examples and usage of functions such as encryption and decryption, string operations, file operations, and SQL injection, PHP project development is very practical and useful.
This article mainly introduces common PHP functions and summarizes examples and usage of functions such as encryption and decryption, string operations, file operations, and SQL injection, PHP project development is very practical and useful.
The examples in this article summarize some functions that are often used in php application development. These functions include character operations, file operations, and other operations. We will share them with you for your reference. The details are as follows:
1. PHP encryption and decryption
The PHP encryption and decryption function can be used to encrypt some useful strings and store them in the database. The function uses base64 and MD5 encryption and decryption.
The Code is as follows:
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 ($ key ))));
Return $ encrypted;
}
}
The usage is as follows:
The Code is as follows:
// Encrypt and decrypt the string "Helloweba welcomes you"
// Encryption:
Echo encryptDecrypt ('Password', 'Welcome to Helloweba ', 0 );
// Decryption:
Echo encryptDecrypt ('Password', 'z0jax4qmwcf + db5TNbp/xwdUM84snRsXvvpXuaCa4Bk = ', 1 );
2. PHP generates random strings
The following functions can be used to generate a random name, temporary password, and other strings:
The Code is as follows:
Function generateRandomString ($ length = 10 ){
$ Characters = '0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxy ';
$ RandomString = '';
For ($ I = 0; $ I <$ length; $ I ++ ){
$ RandomString. = $ characters [rand (0, strlen ($ characters)-1)];
}
Return $ randomString;
}
The usage is as follows:
The Code is as follows:
Echo generateRandomString (20 );
3. PHP obtains the file extension (suffix)
The following functions can quickly obtain the file extension, that is, the suffix.
The Code is as follows:
Function getExtension ($ filename ){
$ Myext = substr ($ filename, strrpos ($ filename ,'.'));
Return str_replace ('.', '', $ myext );
}
The usage is as follows:
The Code is as follows:
$ Filename = 'my document .doc ';
Echo getExtension ($ filename );
4. PHP obtains and formats the file size.
The following functions can be used to obtain the file size and convert it to a readable format such as KB and MB.
The Code is as follows:
Function formatSize ($ size ){
$ Sizes = array ("Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB ", "YB ");
If ($ size = 0 ){
Return ('n'/');
} Else {
Return (round ($ size/pow (1024, ($ I = floor (log ($ size, 1024), 2). $ sizes [$ I]);
}
}
The usage is as follows:
The Code is as follows:
$ Thefile = filesize('test_file ');
Echo formatSize ($ thefile );
5. Replace tag characters in PHP
Sometimes we need to replace the string and template tag with the specified content. The following functions can be used:
The Code is as follows:
Function stringParser ($ string, $ replacer ){
$ Result = str_replace (array_keys ($ replacer), array_values ($ replacer), $ string );
Return $ result;
}
The usage is as follows:
The Code is as follows:
$ 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 );
6. PHP lists the file names in the directory.
If you want to list all the files in the directory, use the following code:
The Code is as follows:
Function listDirFiles ($ DirPath ){
If ($ dir = opendir ($ DirPath )){
While ($ file = readdir ($ dir ))! = False ){
If (! Is_dir ($ DirPath. $ file ))
{
Echo "filename: $ file
";
}
}
}
}
The usage is as follows:
The Code is as follows:
ListDirFiles ('home/some_folder /');
7. PHP retrieves the URL of the current page
The following function obtains the URL of the current page, whether http or https.
The Code is as follows:
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;
}
The usage is as follows:
The Code is as follows:
Echo curPageURL ();
8. Force download of PHP files
Sometimes we do not want the browser to open a file directly, such as a PDF file, but to download the file directly, the following function can force download the file, the function uses the application/octet-stream header type.
The Code is as follows: