Last active
May 21, 2020 08:19
-
-
Save dongnv92/fcb112ba12ff24692a167d603ec049cc to your computer and use it in GitHub Desktop.
PHP TIP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| # PHP Array Search Return Multiple Keys | |
| # PHP Array tìm kiếm và trả về 1 mảng trong Multiple Key | |
| $data = [ | |
| ['id' => 1, 'text' => 'Dưới 20 người'], | |
| ['id' => 2, 'text' => '20 - 50 người'], | |
| ['id' => 3, 'text' => '50 - 100 người'], | |
| ['id' => 4, 'text' => '100 - 300 người'], | |
| ['id' => 5, 'text' => 'Trên 300 người'] | |
| ]; | |
| $search = array_keys(array_column($data, 'id'), $id); // Tìm kiếm và trả về mảng các Key có chứa dữ liệu = $id. | |
| return $data[$search[0]]; // Trả về mảng dữ liệu cần lấy | |
| // Xóa tất cả Folder. File trong thư mục | |
| $dir = 'folder_path'; // ex: '../uploads/media/1 | |
| if (is_dir($dir)) { | |
| array_map('unlink', glob($dir . '/*')); | |
| rmdir($dir); | |
| } | |
| /* | |
| Trong project của bạn 1 chức năng rất cần thiết đó là mã hóa và giải mã password của người dùng hoặc bất kỳ 1 mã số nào đó cần mã hóa đúng không? | |
| Đoạn code dưới đây sử dụng hàm openssl_encrypt() và openssl_decrypt() của PHP để xử lý: | |
| */ | |
| function encrypt_decrypt($string, $action = 'encrypt') | |
| { | |
| $encrypt_method = "AES-256-CBC"; | |
| $secret_key = 'AA74CDCC2BBRT935136HH7B63C27'; // user define private key | |
| $secret_iv = '5fgf5HJ5g27'; // user define secret key | |
| $key = hash('sha256', $secret_key); | |
| $iv = substr(hash('sha256', $secret_iv), 0, 16); // sha256 is hash_hmac_algo | |
| if ($action == 'encrypt') { | |
| $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv); | |
| $output = base64_encode($output); | |
| } else if ($action == 'decrypt') { | |
| $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv); | |
| } | |
| return $output; | |
| } | |
| echo "Your Encrypted password is => ". $pwd = encrypt_decrypt('spaceo', 'encrypt'); | |
| echo '<br/>'; | |
| echo "Your Decrypted password is => ". encrypt_decrypt($pwd, 'decrypt'); | |
| /* | |
| 7. Hiển thị số cùng suffix | |
| Bạn vẫn thường thấy facebook có tính năng hiển thị count list users ex: 10K đúng không? đây là một hàm nhỏ để xử lý thêm suffix vào sau một số: | |
| */ | |
| function formatWithSuffix($input) | |
| { | |
| $suffixes = array('', 'K', 'M', 'B', 'T'); | |
| $suffixIndex = 0; | |
| while(abs($input) >= 1000 && $suffixIndex < sizeof($suffixes)) { | |
| $suffixIndex++; | |
| $input /= 1000; | |
| } | |
| return ($input > 0 ? floor($input * 1000) / 1000 : ceil($input * 1000) / 1000). $suffixes[$suffixIndex]; | |
| } | |
| echo formatWithSuffix('10000'); | |
| echo formatWithSuffix('10000000'); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment