- 相關(guān)推薦
PHP函數(shù)知識(shí)總結(jié)
任何有效的 php 代碼都有可能出現(xiàn)在函數(shù)內(nèi)部,甚至包括其它函數(shù)和類定義。那么PHP函數(shù)知識(shí)都有哪些呢?以下僅供參考!
1、PHP加密解密
PHP加密和解密函數(shù)可以用來加密一些有用的字符串存放在數(shù)據(jù)庫里,并且通過可逆解密字符串,該函數(shù)使用了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生成隨機(jī)字符串
當(dāng)我們需要生成一個(gè)隨機(jī)名字,臨時(shí)密碼等字符串時(shí)可以用到下面的函數(shù):
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獲取文件擴(kuò)展名(后綴)
以下函數(shù)可以快速獲取文件的擴(kuò)展名即后綴。
function getExtension($filename){
$myext = substr($filename, strrpos($filename, '.'));
return str_replace('.','',$myext);
}
使用方法如下:
$filename = '我的文檔.doc';
echo getExtension($filename);
4、PHP獲取文件大小并格式化
以下使用的函數(shù)可以獲取文件的大小,并且轉(zhuǎn)換成便于閱讀的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替換標(biāo)簽字符
有時(shí)我們需要將字符串、模板標(biāo)簽替換成指定的內(nèi)容,可以用到下面的函數(shù):
function stringParser($string,$replacer){
$result = str_replace(array_keys($replacer), array_values($replacer),$string);
return $result;
}
使用方法如下:
$string = 'The anchor text{/b} is the actual word{/b} or words used {br}to describe the link {br}itself';
$replace_array = array('' => '','{/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獲取當(dāng)前頁面URL
以下函數(shù)可以獲取當(dāng)前頁面的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強(qiáng)制下載文件
有時(shí)我們不想讓瀏覽器直接打開文件,如PDF文件,而是要直接下載文件,那么以下函數(shù)可以強(qiáng)制下載文件,函數(shù)中使用了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截取字符串長(zhǎng)度
我們經(jīng)常會(huì)遇到需要截取字符串(含中文漢字)長(zhǎng)度的情況,比如標(biāo)題顯示不能超過多少字符,超出的長(zhǎng)度用…表示,以下函數(shù)可以滿足你的需求。
/*
Utf-8、gb2312都支持的漢字截取函數(shù)
cut_str(字符串, 截取長(zhǎng)度, 開始長(zhǎng)度, 編碼);
編碼默認(rèn)為 utf-8
開始長(zhǎng)度默認(rèn)為 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插件實(shí)現(xiàn)的加載圖片和頁面效果";
echo cutStr($str,16);
10、PHP獲取客戶端真實(shí)IP
我們經(jīng)常要用數(shù)據(jù)庫記錄用戶的IP,以下代碼可以獲取客戶端真實(shí)的IP:
//獲取用戶真實(shí)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();
【PHP函數(shù)知識(shí)總結(jié)】相關(guān)文章:
關(guān)于PHP數(shù)組函數(shù)知識(shí)09-16
PHP常用函數(shù)總結(jié)10-21
php常用的url處理函數(shù)總結(jié)07-09
PHP類與構(gòu)造函數(shù)07-01
PHP函數(shù)的區(qū)別及用法10-27
PHP內(nèi)部函數(shù)的定義07-04