- 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;
- }
- }
-
複製代碼使用方法:
- //以下是將字串“helloweba歡迎您”分別加密和解密
- //加密:
- echo encryptdecrypt('password', 'helloweba歡迎您',0);
- //解密:
- echo encryptdecrypt('password', 'z0jax4qmwcf+db5tnbp/xwdum84snrsxvvpxuaca4bk=',1);
複製代碼2、php產生隨機字串 當需要產生一個隨機名字,臨時密碼等字串時,使用如下函數:
- function generaterandomstring($length = 10) {
- $characters = '0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz';
- $randomstring = '';
- for ($i = 0; $i < $length; $i++) {
- $randomstring .= $characters[rand(0, strlen($characters) - 1)];
- }
- return $randomstring;
- }
複製代碼使用方法:
- echo generaterandomstring(20);
複製代碼3、php擷取副檔名(尾碼)快速擷取檔案的副檔名即尾碼。
- function getextension($filename){
- $myext = substr($filename, strrpos($filename, '.'));
- return str_replace('.','',$myext);
- }
複製代碼使用方法:
- $filename = '我的文件.doc';
- echo getextension($filename);
複製代碼4、php擷取檔案大小並格式化擷取檔案的大小,並且轉換成便於閱讀的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(1024, ($i = floor(log($size, 1024)))), 2) . $sizes[$i]);
- }
- }
複製代碼使用方法:
- $thefile = filesize('test_file.mp3');
- echo formatsize($thefile);
複製代碼5、php替換標籤字元將字串、模板標籤替換成指定的內容,函數:
- function stringparser($string,$replacer){
- $result = str_replace(array_keys($replacer), array_values($replacer),$string);
- return $result;
- }
複製代碼使用方法:
$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列出目錄下的檔案名稱列出目錄下的所有檔案:
- function listdirfiles($dirpath){
- if($dir = opendir($dirpath)){
- while(($file = readdir($dir))!== false){
- if(!is_dir($dirpath.$file))
- {
- echo "filename: $file
";
- }
- }
- }
- }
-
複製代碼使用方法:listdirfiles('home/some_folder/'); 7、php擷取當前頁面url以下函數可以擷取當前頁面的url,不管是http還是https。
- 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;
- }
-
複製代碼使用方法:
- echo curpageurl();
複製代碼8、php強制下載檔案不想讓瀏覽器直接開啟檔案,如pdf檔案,而是要直接下載檔案,那麼以下函數可以強制下載檔案,函數中使用了application/octet-stream頭類型。
- function download($filename){
- if ((isset($filename))&&(file_exists($filename))){
- header("content-length: ".filesize($filename));
- header('content-type: application/octet-stream');
- header('content-disposition: attachment; filename="' . $filename . '"');
- readfile("$filename");
- } else {
- echo "looks like file does not exist!";
- }
- }
-
複製代碼使用方法:
- download('/down/test_45f73e852.zip');
複製代碼9、php截取字串長度需要截取字串(含中文漢字)長度的情況,比如標題顯示不能超過多少字元,超出的長度用…表示,以下函數可以滿足你的需求。
/*
- utf-8、gb2312都支援的漢字截取函數
- cut_str(字串, 截取長度, 開始長度, 編碼);
- 編碼預設為 utf-8
- 開始長度預設為 0
- */
- function cutstr($string, $sublen, $start = 0, $code = 'utf-8'){
- if($code == 'utf-8'){
- $pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
- preg_match_all($pa, $string, $t_string);
if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen))."...";
- return join('', array_slice($t_string[0], $start, $sublen));
- }else{
- $start = $start*2;
- $sublen = $sublen*2;
- $strlen = strlen($string);
- $tmpstr = '';
for($i=0; $i<$strlen; $i++){
- if($i>=$start && $i<($start+$sublen)){
- if(ord(substr($string, $i, 1))>129){
- $tmpstr.= substr($string, $i, 2);
- }else{
- $tmpstr.= substr($string, $i, 1);
- }
- }
- if(ord(substr($string, $i, 1))>129) $i++;
- }
- if(strlen($tmpstr)<$strlen ) $tmpstr.= "...";
- return $tmpstr;
- }
- }
-
複製代碼使用方法:
- $str = "jquery外掛程式實現的載入圖片和頁面效果";
- echo cutstr($str,16);
複製代碼10、php擷取用戶端真實ip經常要用資料庫記錄使用者的ip,擷取用戶端真實的ip:
- //擷取使用者真實ip
- function getip() {
- if (getenv("http_client_ip") && strcasecmp(getenv("http_client_ip"), "unknown"))
- $ip = getenv("http_client_ip");
- else
- if (getenv("http_x_forwarded_for") && strcasecmp(getenv("http_x_forwarded_for"), "unknown"))
- $ip = getenv("http_x_forwarded_for");
- else
- if (getenv("remote_addr") && strcasecmp(getenv("remote_addr"), "unknown"))
- $ip = getenv("remote_addr");
- else
- if (isset ($_server['remote_addr']) && $_server['remote_addr'] && strcasecmp($_server['remote_addr'], "unknown"))
- $ip = $_server['remote_addr'];
- else
- $ip = "unknown";
- return ($ip);
- }
-
複製代碼使用方法:
- echo getip();
複製代碼11、php防止sql注入在查詢資料庫時,出於安全考慮,需要過濾一些非法字元防止sql惡意注入,請看一下函數:
- function injcheck($sql_str) {
- $check = preg_match('/select|insert|update|delete|\'|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile/', $sql_str);
- if ($check) {
- echo '非法字元!!';
- exit;
- } else {
- return $sql_str;
- }
- }
-
複製代碼使用方法如下:
- function message($msgtitle,$message,$jumpurl){
- $str = '';
- $str .= '';
- $str .= '';
- $str .= '';
- $str .= '頁面提示';
- $str .= '';
- $str .= '';
- $str .= '';
- $str .= '';
- $str .= '
'.$msgtitle.'';
- $str .= '';
- $str .= '
'.$message.'';
- $str .= '
系統將在 3 秒後自動跳轉,如果不想等待,直接點擊 這裡 跳轉 ';
- $str .= "";
- $str .= '';
- $str .= '';
- $str .= '';
- $str .= '';
- echo $str;
- }
-
複製代碼使用方法:
- function changetimetype($seconds) {
- if ($seconds > 3600) {
- $hours = intval($seconds / 3600);
- $minutes = $seconds % 3600;
- $time = $hours . ":" . gmstrftime('%m:%s', $minutes);
- } else {
- $time = gmstrftime('%h:%m:%s', $seconds);
- }
- return $time;
- }
-
複製代碼使用方法: $seconds = 3712; echo changetimetype($seconds); |