php: Organize some useful custom functions

伊谢尔伦
Release: 2023-03-11 13:10:01
Original
1190 people have browsed it

1. PHP encryption and decryption functions

PHP encryption and decryption functions can be used to encrypt some useful strings stored in the database, and reversibly decrypt the strings. This 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))), "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:

//以下是将字符串“Helloweba欢迎您”分别加密和解密 
//加密: 
echo encryptDecrypt('password', 'Helloweba欢迎您',0); 
//解密: 
echo encryptDecrypt('password', 'z0JAx4qMwcF+db5TNbp/xwdUM84snRsXvvpXuaCa4Bk=',1);
Copy after login

2. PHP generates random strings

When we need to generate a random name, temporary password and other strings, we can use the following Function:

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:

echo generateRandomString(20);
Copy after login

3. PHP gets the file extension (suffix)

The following function can quickly get the file extension, that is, the suffix.

function getExtension($filename){ 
  $myext = substr($filename, strrpos($filename, &#39;.&#39;)); 
  return str_replace(&#39;.&#39;,&#39;&#39;,$myext); 
}
Copy after login

The usage method is as follows:

$filename = &#39;我的文档.doc&#39;; 
echo getExtension($filename);
Copy after login

4. PHP gets the file size and formats it

The function used below can get the file size and convert it into KB that is easy to read. , MB and other formats.

function formatSize($size) { 
    $sizes = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB"); 
    if ($size == 0) {  
        return(&#39;n/a&#39;);  
    } else { 
      return (round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . $sizes[$i]);  
    } 
}
Copy after login

The usage method is as follows: View the demo

$thefile = filesize(&#39;test_file.mp3&#39;); 
echo formatSize($thefile);
Copy after login

5. PHP replacement tag characters

Sometimes we need to replace strings and template tags with specified content, you can use the following function:

function stringParser($string,$replacer){ 
    $result = str_replace(array_keys($replacer), array_values($replacer),$string); 
    return $result; 
}
Copy after login

The usage is as follows:

$string = &#39;The {b}anchor text{/b} is the {b}actual word{/b} or words used {br}to describe the link {br}itself&#39;; 
$replace_array = array(&#39;{b}&#39; => &#39;<b>&#39;,&#39;{/b}&#39; => &#39;</b>&#39;,&#39;{br}&#39; => &#39;<br />&#39;); 
 
echo stringParser($string,$replace_array);
Copy after login

6. PHP lists the file names in the directory

If you want to list the directory For all files under, 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 />"; 
                } 
         } 
    } 
}
Copy after login

The usage method is as follows:

listDirFiles(&#39;home/some_folder/&#39;);
Copy after login

The above is the detailed content of php: Organize some useful custom functions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template