国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

轉載:非常實用的PHP常用函數匯總

Little_XM / 1097人閱讀

摘要:使用方法如下截取字符串長度我們經常會遇到需要截取字符串含中文漢字長度的情況,比如標題顯示不能超過多少字符,超出的長度用表示,以下函數可以滿足你的需求。,下載創建一個目錄樹

1、PHP加密解密

PHP加密和解密函數可以用來加密一些有用的字符串存放在數據庫里,并且通過可逆解密字符串,該函數使用了base64和MD5加密和解密。

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; 
    } 
}

使用方法如下:

echo injCheck("1 or 1=1");

12、PHP頁面提示與跳轉
我們在進行表單操作時,有時為了友好需要提示用戶操作結果,并跳轉到相關頁面,請看以下函數:

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; }

使用方法如下:

message("操作提示","操作成功!","https://segmentfault.com/");

13、PHP計算時長
我們在處理時間時,需要計算當前時間距離某個時間點的時長,如計算客戶端運行時長,通常用hh:mm:ss表示

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);

希望本文所述對大家的PHP程序設計有所幫助。
下面特為大家加點料希望大家能喜歡:
本文匯總了常用的PHP函數,包括獲取客戶端IP,字符串截取,下載等,詳情請查看如下代碼:

 1) { // 多字節字符
  $return .= "%u" . strtoupper(bin2hex(mb_convert_encoding($str, "UCS-2", $encoding)));
 } else {
  $return .= "%" . strtoupper(bin2hex($str));
 }
 }
 return $return;
}
/**
 * php 實現 js unescape函數
 * @param [type] $str [description]
 * @return [type]  [description]
 */
function unescape($str) {
 $str = rawurldecode($str);
 preg_match_all("/(?:%u.{4})|.{4};|&#d+;|.+/U",$str,$r);
 $ar = $r[0];
 foreach($ar as $k=>$v) {
  if(substr($v,0,2) == "%u"){
   $ar[$k] = iconv("UCS-2","utf-8//IGNORE",pack("H4",substr($v,-4)));
  } elseif(substr($v,0,3) == "") {
   $ar[$k] = iconv("UCS-2","utf-8",pack("H4",substr($v,3,-1)));
  } elseif(substr($v,0,2) == "&#") {
   echo substr($v,2,-1)."";
   $ar[$k] = iconv("UCS-2","utf-8",pack("n",substr($v,2,-1)));
  }
 }
 return join("",$ar);
}
 
/**
 * 數字轉人名幣
 * @param [type] $num [description]
 * @return [type]  [description]
 */
function num2rmb ($num) {
 $c1 = "零壹貳叁肆伍陸柒捌玖";
 $c2 = "分角元拾佰仟萬拾佰仟億";
 $num = round($num, 2);
 $num = $num * 100;
 if (strlen($num) > 10) {
  return "oh,sorry,the number is too long!";
 }
 $i = 0;
 $c = "";
 while (1) {
  if ($i == 0) {
   $n = substr($num, strlen($num)-1, 1);
  } else {
   $n = $num % 10;
  }
  $p1 = substr($c1, 3 * $n, 3);
  $p2 = substr($c2, 3 * $i, 3);
  if ($n != "0" || ($n == "0" && ($p2 == "億" || $p2 == "萬" || $p2 == "元"))) {
   $c = $p1 . $p2 . $c;
  } else {
   $c = $p1 . $c;
  }
  $i = $i + 1;
  $num = $num / 10;
  $num = (int)$num;
  if ($num == 0) {
   break;
  }
 }
 $j = 0;
 $slen = strlen($c);
 while ($j < $slen) {
  $m = substr($c, $j, 6);
  if ($m == "零元" || $m == "零萬" || $m == "零億" || $m == "零零") {
   $left = substr($c, 0, $j);
   $right = substr($c, $j + 3);
   $c = $left . $right;
   $j = $j-3;
   $slen = $slen-3;
  }
  $j = $j + 3;
 }
 if (substr($c, strlen($c)-3, 3) == "零") {
  $c = substr($c, 0, strlen($c)-3);
 } // if there is a "0" on the end , chop it out
 return $c . "整";
}
 
/**
 * 特殊的字符
 * @param [type] $str [description]
 * @return [type]  [description]
 */
function makeSemiangle($str) {
 $arr = array(
  "0" => "0", "1" => "1", "2" => "2", "3" => "3", "4" => "4",
  "5" => "5", "6" => "6", "7" => "7", "8" => "8", "9" => "9",
  "A" => "A", "B" => "B", "C" => "C", "D" => "D", "E" => "E",
  "F" => "F", "G" => "G", "H" => "H", "I" => "I", "J" => "J",
  "K" => "K", "L" => "L", "M" => "M", "N" => "N", "O" => "O",
  "P" => "P", "Q" => "Q", "R" => "R", "S" => "S", "T" => "T",
  "U" => "U", "V" => "V", "W" => "W", "X" => "X", "Y" => "Y",
  "Z" => "Z", "a" => "a", "b" => "b", "c" => "c", "d" => "d",
  "e" => "e", "f" => "f", "g" => "g", "h" => "h", "i" => "i",
  "j" => "j", "k" => "k", "l" => "l", "m" => "m", "n" => "n",
  "o" => "o", "p" => "p", "q" => "q", "r" => "r", "s" => "s",
  "t" => "t", "u" => "u", "v" => "v", "w" => "w", "x" => "x",
  "y" => "y", "z" => "z",
  "(" => "(", ")" => ")", "〔" => "[", "〕" => "]", "【" => "[",
  "】" => "]", "〖" => "[", "〗" => "]", "{" => "{", "}" => "}", "《" => "<",
  "》" => ">",
  "%" => "%", "+" => "+", "—" => "-", "-" => "-", "~" => "-",
  ":" => ":", "。" => ".", "、" => ",", "," => ".", "、" => ".",
  ";" => ";", "?" => "?", "!" => "!", "…" => "-", "‖" => "|",
  "”" => """, "“" => """, """ => "`", "‘" => "`", "|" => "|", "〃" => """,
  " " => " ","." => ".");
 return strtr($str, $arr);
}
 
/**
 * 下載
 * @param [type] $filename [description]
 * @param string $dir  [description]
 * @return [type]   [description]
 */
function downloads($filename,$dir="./"){
 $filepath = $dir.$filename;
 if (!file_exists($filepath)){
  header("Content-type: text/html; charset=utf-8");
  echo "File not found!";
  exit;
 } else {
  $file = fopen($filepath,"r");
  Header("Content-type: application/octet-stream");
  Header("Accept-Ranges: bytes");
  Header("Accept-Length: ".filesize($filepath));
  Header("Content-Disposition: attachment; filename=".$filename);
  echo fread($file, filesize($filepath));
  fclose($file);
 }
}
 
/**
 * 創建一個目錄樹
 * @param [type] $dir [description]
 * @param integer $mode [description]
 * @return [type]  [description]
 */
function mkdirs($dir, $mode = 0777) {
 if (!is_dir($dir)) {
  mkdirs(dirname($dir), $mode);
  return mkdir($dir, $mode);
 }
 return true;
}

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/22528.html

相關文章

  • 前端常用插件、工具類庫匯總

    摘要:頁面調試騰訊開發維護的代碼調試發布,錯誤監控上報,用戶問題定位。同樣是由騰訊開發維護的代碼調試工具,是針對移動端的調試工具。前端業務代碼工具庫。動畫庫動畫庫,也是目前通用的動畫庫。 本人微信公眾號:前端修煉之路,歡迎關注 本篇文章整理自己使用過的和看到過的一些插件和工具,方便日后自己查找和使用。 另外,感謝白小明,文中很多的工具來源于此。 彈出框 layer:http://layer....

    GitCafe 評論0 收藏0
  • Java開發

    摘要:大多數待遇豐厚的開發職位都要求開發者精通多線程技術并且有豐富的程序開發調試優化經驗,所以線程相關的問題在面試中經常會被提到。將對象編碼為字節流稱之為序列化,反之將字節流重建成對象稱之為反序列化。 JVM 內存溢出實例 - 實戰 JVM(二) 介紹 JVM 內存溢出產生情況分析 Java - 注解詳解 詳細介紹 Java 注解的使用,有利于學習編譯時注解 Java 程序員快速上手 Kot...

    LuDongWei 評論0 收藏0

發表評論

0條評論

Little_XM

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<