php 功能函数集
1.獲取頁(yè)面閉合帶id標(biāo)簽數(shù)據(jù)
View Code 1 <?php 2 header("Content-type: text/html; charset=utf-8"); 3 /** 4 * $tag_id HTML tag_id like id="abc" 5 * $url web url 6 * $tag HTML tag 7 * $data HTML data if $url set to false 8 * @example echo getWebTag('id="nav"','http://mail.163.com/html/mail_intro/','ul'); 9 */ 10 function getWebTag($tag_id,$url=false,$tag='div',$data=false){ 11 if($url !== false){ 12 $data = file_get_contents( $url ); 13 } 14 $charset_pos = stripos($data,'charset'); 15 if($charset_pos) { 16 if(stripos($data,'utf-8',$charset_pos)) { 17 $data = iconv('utf-8','utf-8',$data); 18 }else if(stripos($data,'gb2312',$charset_pos)) { 19 $data = iconv('gb2312','utf-8',$data); 20 }else if(stripos($data,'gbk',$charset_pos)) { 21 $data = iconv('gbk','utf-8',$data); 22 } 23 } 24 25 preg_match_all('/<'.$tag.'/i',$data,$pre_matches,PREG_OFFSET_CAPTURE); //獲取所有div前綴 26 preg_match_all('/<\/'.$tag.'/i',$data,$suf_matches,PREG_OFFSET_CAPTURE); //獲取所有div后綴 27 $hit = strpos($data,$tag_id); 28 if($hit == -1) return false; //未命中 29 $divs = array(); //合并所有div 30 foreach($pre_matches[0] as $index=>$pre_div){ 31 $divs[(int)$pre_div[1]] = 'p'; 32 $divs[(int)$suf_matches[0][$index][1]] = 's'; 33 } 34 35 //對(duì)div進(jìn)行排序 36 $sort = array_keys($divs); 37 asort($sort); 38 39 $count = count($pre_matches[0]); 40 foreach($pre_matches[0] as $index=>$pre_div){ 41 //<div $hit <div+1 時(shí)div被命中 42 if(($pre_matches[0][$index][1] < $hit) && ($hit < $pre_matches[0][$index+1][1])){ 43 $deeper = 0; 44 //彈出被命中div前的div 45 while(array_shift($sort) != $pre_matches[0][$index][1] && ($count--)) continue; 46 //對(duì)剩余div進(jìn)行匹配,若下一個(gè)為前綴,則向下一層,$deeper加1, 47 //否則后退一層,$deeper減1,$deeper為0則命中匹配,計(jì)算div長(zhǎng)度 48 foreach($sort as $key){ 49 if($divs[$key] == 'p') $deeper++; 50 else if($deeper == 0) { 51 $length = $key-$pre_matches[0][$index][1]; 52 break; 53 }else { 54 $deeper--; 55 } 56 } 57 $hitDivString = substr($data,$pre_matches[0][$index][1],$length).'</'.$tag.'>'; 58 break; 59 } 60 } 61 return $hitDivString; 62 } 63 64 echo getWebTag('id="nav"','http://mail.163.com/html/mail_intro/','ul'); 65 echo getWebTag('id="homeBanners"','http://mail.163.com/html/mail_intro/'); 66 echo getWebTag('id="performance"','http://mail.163.com/html/mail_intro/','section'); 67 68 //End_php1.1 由(1)改進(jìn)為獲取頁(yè)面任意標(biāo)簽,參考《顛覆想象的php解析獲取跨域HTML標(biāo)簽》
View Code 1 <?php 2 header("Content-type: text/html; charset=utf-8"); 3 /** 4 * $tag_id HTML tag_id like id="abc" 5 * $url web url 6 * $tag HTML tag 7 * $data HTML data if $url set to false 8 * $first Only get the first match 9 * @example 10 var_dump(getWebTag('id="nav"','http://mail.163.com/html/mail_intro/','ul')); 11 */ 12 function getWebTag($tag_id,$url=false,$tag='div',$data=false,$first=false){ 13 //默認(rèn)采用URL獲取數(shù)據(jù) 14 if($url !== false){ 15 $data = file_get_contents( $url ); 16 } 17 //頁(yè)面編碼判定及轉(zhuǎn)碼 18 $charset_pos = stripos($data,'charset'); 19 if($charset_pos) { 20 if(stripos($data,'charset=utf-8',$charset_pos)) { 21 $data = iconv('utf-8','utf-8',$data); 22 }else if(stripos($data,'charset=gb2312',$charset_pos)) { 23 $data = iconv('gb2312','utf-8',$data); 24 }else if(stripos($data,'charset=gbk',$charset_pos)) { 25 $data = iconv('gbk','utf-8',$data); 26 } 27 } 28 29 //匹配命中標(biāo)簽至數(shù)組$hits 30 preg_match_all('/<'.$tag.'[^<]*?'.$tag_id.'/i',$data,$hits,PREG_OFFSET_CAPTURE); 31 if(count($hits[0]) === 0) { //未命中,直接返回 32 return '沒(méi)有匹配項(xiàng)!'; 33 } 34 35 preg_match_all('/<'.$tag.'/i',$data,$pre_matches,PREG_OFFSET_CAPTURE); //獲取所有HTML標(biāo)簽前綴 36 preg_match_all('/<\/'.$tag.'/i',$data,$suf_matches,PREG_OFFSET_CAPTURE); //獲取所有HTML標(biāo)簽后綴 37 38 //判斷是否<div></div>格式,是則添加結(jié)束標(biāo)簽,否則為false; 注:img、input等可能不是這種格式,此時(shí)$suf_matches[0]為空。 39 if(!empty($suf_matches[0])) $endTag = '</'.$tag.'>'; 40 else $endTag = false; 41 42 //合并所有HTML標(biāo)簽 43 $htmltags = array(); 44 if($endTag !== false){ 45 foreach($pre_matches[0] as $index=>$pre_div){ 46 $htmltags[(int)$pre_matches[0][$index][1]] = 'p'; 47 $htmltags[(int)$suf_matches[0][$index][1]] = 's'; 48 } 49 }else{ 50 foreach($pre_matches[0] as $index=>$pre_div){ 51 //非<div></div>格式,獲取前綴下標(biāo)后的第一個(gè)>作為標(biāo)簽結(jié)束 52 $suf_matches[0][$index][1] = stripos($data,'>',$pre_matches[0][$index][1])+1; 53 54 $htmltags[(int)$pre_matches[0][$index][1]] = 'p'; 55 $htmltags[(int)$suf_matches[0][$index][1]] = 's'; 56 } 57 } 58 //對(duì)所有HTML標(biāo)簽按index進(jìn)行排序 59 $sort = array_keys($htmltags); 60 asort($sort); 61 62 //開(kāi)始獲取命中字符串 63 $hitTagStrings = array(); 64 foreach($hits[0] as $hit){ 65 $hit = $hit[1]; //獲取命中index 66 67 $count = count($sort); //循環(huán)控制,$count--避免無(wú)限循環(huán) 68 foreach($pre_matches[0] as $index=>$pre_div){ 69 //最后一個(gè)$pre_matches[0][$index+1]會(huì)造成數(shù)組出界,因此設(shè)置其index等于總長(zhǎng)度 70 if(!isset($pre_matches[0][$index+1][1])) $pre_matches[0][$index+1][1] = strlen($data); 71 72 //<div $hit <div+1 時(shí)div被命中 73 if(($pre_matches[0][$index][1] <= $hit) && ($hit < $pre_matches[0][$index+1][1])){ 74 $deeper = 0; 75 //彈出被命中HTML標(biāo)簽前的所有HTML標(biāo)簽 76 while(array_shift($sort) != $pre_matches[0][$index][1] && ($count--)) continue; 77 //對(duì)剩余HTML標(biāo)簽進(jìn)行匹配,若下一個(gè)為前綴(p),則向下一層,$deeper加1, 78 //否則后退一層,$deeper減1,$deeper為0則命中匹配結(jié)束標(biāo)記,計(jì)算div長(zhǎng)度 79 foreach($sort as $key){ 80 if($htmltags[$key] == 'p') { //進(jìn)入子層 81 $deeper++; 82 }else if($deeper == 0) { //碰到結(jié)束標(biāo)記 83 $length = $key-$pre_matches[0][$index][1]; //長(zhǎng)度等于結(jié)束標(biāo)記index 減去 前綴index 84 break; 85 }else { //碰到子層結(jié)束標(biāo)記 86 $deeper--; 87 } 88 } 89 $hitTagStrings[] = substr($data,$pre_matches[0][$index][1],$length).$endTag; 90 break; 91 } 92 } 93 //若只獲取第一個(gè)匹配項(xiàng),退出循環(huán) 94 if($first && count($hitTagStrings) == 1) break; 95 } 96 97 return $hitTagStrings; 98 } 99 100 //直接用例 101 var_dump(getWebTag('id="nav"','http://mail.163.com/html/mail_intro/','ul')); 102 103 /* //注釋這句即可顯示 104 //ajax請(qǐng)求用例,必要參數(shù):dataType:'json',type:'POST' 105 $tag_id = urldecode($_POST['tag_id']); 106 $url = urldecode($_POST['url']); 107 $tag = isset($_POST['tag'])? urldecode($_POST['tag']) : 'div'; 108 $data = urldecode($_POST['data']); 109 $first = (urldecode($_POST['first']) == 'checked')? true : false; 110 foreach($_POST as $key => $value){ 111 if($value == 'EmPtYValue') $$key = false; 112 } 113 echo json_encode(getWebTag($tag_id,$url,$tag,$data,$first)); 114 //*/ 115 116 //End_php?
2.虛擬POST數(shù)據(jù)至遠(yuǎn)程服務(wù)器并獲取返回?cái)?shù)據(jù)
View Code 1 <?php 2 header("Content-type: text/html; charset=utf-8"); 3 /** 4 * $url web url 5 * $post POST data 6 * @example 7 $data = array ( 8 'type' => 'text', 9 'inputValue' => '哈哈' 10 ); 11 $result = Post('http://tool.anzhuoxiazai.com:80//servlet/QRServlet', $data); 12 echo str_replace("src='","src='http://tool.anzhuoxiazai.com/",$result); 13 */ 14 function Post($url, $post = null) { 15 $context = array(); 16 if (is_array($post)) { 17 ksort($post); 18 $context['http'] = array ( 19 'timeout'=>60, 20 'method' => 'POST', 21 'content' => http_build_query($post) 22 ); 23 } 24 return file_get_contents($url, false, stream_context_create($context)); 25 } 26 27 $data = array ( 28 'type' => 'text', 29 'inputValue' => '哈哈' 30 ); 31 $result = Post('http://tool.anzhuoxiazai.com:80//servlet/QRServlet', $data); 32 echo str_replace("src='","src='http://tool.anzhuoxiazai.com/",$result); 33 //End_php?
3.文件夾復(fù)制
View Code 1 /** 2 * Copy file or folder from source to destination, it can do 3 * recursive copy as well and is very smart 4 * It recursively creates the dest file or directory path if there weren't exists 5 * Situtaions : 6 * - Src:/home/test/file.txt ,Dst:/home/test/b ,Result:/home/test/b -> If source was file copy file.txt name with b as name to destination 7 * - Src:/home/test/file.txt ,Dst:/home/test/b/ ,Result:/home/test/b/file.txt -> If source was file Creates b directory if does not exsits and copy file.txt into it 8 * - Src:/home/test ,Dst:/home/ ,Result:/home/test/** -> If source was directory copy test directory and all of its content into dest 9 * - Src:/home/test/ ,Dst:/home/ ,Result:/home/**-> if source was direcotry copy its content to dest 10 * - Src:/home/test ,Dst:/home/test2 ,Result:/home/test2/** -> if source was directoy copy it and its content to dest with test2 as name 11 * - Src:/home/test/ ,Dst:/home/test2 ,Result:->/home/test2/** if source was directoy copy it and its content to dest with test2 as name 12 * @todo 13 * - Should have rollback technique so it can undo the copy when it wasn't successful 14 * - Auto destination technique should be possible to turn off 15 * - Supporting callback function 16 * - May prevent some issues on shared enviroments : http://us3.php.net/umask 17 * @param $source //file or folder 18 * @param $dest ///file or folder 19 * @param $options //folderPermission,filePermission 20 * @return boolean 21 */ 22 function smartCopy($source, $dest, $options=array('folderPermission'=>0755,'filePermission'=>0755)) 23 { 24 $result=false; 25 26 if (is_file($source)) { 27 if ($dest[strlen($dest)-1]=='/') { 28 if (!file_exists($dest)) { 29 cmfcDirectory::makeAll($dest,$options['folderPermission'],true); 30 } 31 $__dest=$dest."/".basename($source); 32 } else { 33 $__dest=$dest; 34 } 35 $result=copy($source, $__dest); 36 chmod($__dest,$options['filePermission']); 37 38 } elseif(is_dir($source)) { 39 if ($dest[strlen($dest)-1]=='/') { 40 if ($source[strlen($source)-1]=='/') { 41 //Copy only contents 42 } else { 43 //Change parent itself and its contents 44 $dest=$dest.basename($source); 45 @mkdir($dest); 46 chmod($dest,$options['filePermission']); 47 } 48 } else { 49 if ($source[strlen($source)-1]=='/') { 50 //Copy parent directory with new name and all its content 51 @mkdir($dest,$options['folderPermission']); 52 chmod($dest,$options['filePermission']); 53 } else { 54 //Copy parent directory with new name and all its content 55 @mkdir($dest,$options['folderPermission']); 56 chmod($dest,$options['filePermission']); 57 } 58 } 59 60 $dirHandle=opendir($source); 61 while($file=readdir($dirHandle)) 62 { 63 if($file!="." && $file!="..") 64 { 65 if(!is_dir($source."/".$file)) { 66 $__dest=$dest."/".$file; 67 } else { 68 $__dest=$dest."/".$file; 69 } 70 //echo "$source/$file ||| $__dest<br />"; 71 $result=smartCopy($source."/".$file, $__dest, $options); 72 } 73 } 74 closedir($dirHandle); 75 76 } else { 77 $result=false; 78 } 79 return $result; 80 }?
4.文件遍歷(可匹配模式)
View Code 1 /** 2 * 遍歷目錄并獲取所有目錄即文件,以數(shù)組array('dirs'=>$dirs,'files'=>$files)方式返回。 3 * @param $dir //搜索目錄 4 * @param $pattern // '*'搜索全部文件,可以智能匹配,如*.jpg 搜索jpg文件,*.{jpg,png}搜索jpg和png文件,區(qū)分大小寫(xiě)!! 5 * @param $skip //排除遍歷文件,如"*.{jpg,png}"排除.jpg和.png類(lèi)型文件 6 * @param $subInclude //默認(rèn)遍歷子目錄,$subInclude設(shè)置為false則僅遍歷當(dāng)前目錄 7 * @param @flag //glob函數(shù)的標(biāo)記,有效標(biāo)記如下: 8 GLOB_MARK - 在每個(gè)返回的項(xiàng)目中加一個(gè)斜線(xiàn) 9 GLOB_NOSORT - 按照文件在目錄中出現(xiàn)的原始順序返回(不排序) 10 GLOB_NOCHECK - 如果沒(méi)有文件匹配則返回用于搜索的模式 11 GLOB_NOESCAPE - 反斜線(xiàn)不轉(zhuǎn)義元字符 12 GLOB_BRACE - 擴(kuò)充 {a,b,c} 來(lái)匹配 'a','b' 或 'c' 13 GLOB_ONLYDIR - 僅返回與模式匹配的目錄項(xiàng) 14 */ 15 function scandir_through($dir,$pattern='*',$skip=false,$subInclude=true,$flag=GLOB_BRACE){ 16 $dirs = array(); 17 $files = array(); 18 //獲取當(dāng)前目錄下所有文件及文件夾 19 $items = glob($dir . '/*'); 20 21 //遍歷所有項(xiàng)目,若設(shè)置$subInclude為true,則繼續(xù)遍歷子目錄 22 for ($i = 0; $i < count($items); $i++) { 23 if ($subInclude && is_dir($items[$i])) { 24 $dirs[] = iconv('gb2312','utf-8',$items[$i]); 25 $add = glob($items[$i] . '/*'); 26 if($add === false) $add = array(); 27 $items = array_merge($items, $add); 28 }else { 29 $slash = strrpos($items[$i],'/'); 30 $dir = substr($items[$i],0,$slash); 31 //若當(dāng)前文件匹配文件查找模式$pattern,則加入$files數(shù)組中 32 if(in_array($items[$i],glob($dir.'/'.$pattern,$flag)) && (($skip===false) || !in_array($items[$i],glob($dir.'/'.$skip,$flag)))) { 33 $items[$i] = iconv('gb2312','utf-8',$items[$i]); 34 $file = substr($items[$i],$slash+1); 35 $files[] = $items[$i]; 36 } 37 } 38 } 39 return array('dirs'=>$dirs,'files'=>$files); 40 }?
5.zip壓縮文件夾
View Code 1 /** 2 * 打包文件夾成zip文件函數(shù) 3 * @param $path //打包文件夾路徑 4 * @param $zip //ZipArchive 對(duì)象 5 */ 6 function addFileToZip($path, $zip) { 7 $handler = opendir($path); //打開(kāi)當(dāng)前文件夾由$path指定。 8 /* 9 循環(huán)的讀取文件夾下的所有文件和文件夾 10 其中$filename = readdir($handler)是每次循環(huán)的時(shí)候?qū)⒆x取的文件名賦值給$filename, 11 為了不陷于死循環(huán),所以還要讓$filename !== false。 12 一定要用!==,因?yàn)槿绻硞€(gè)文件名如果叫'0',或者某些被系統(tǒng)認(rèn)為是代表false,用!=就會(huì)停止循環(huán) 13 */ 14 while (($filename = readdir($handler)) !== false) { 15 if ($filename != "." && $filename != "..") {//文件夾文件名字為'.'和‘..’,不要對(duì)他們進(jìn)行操作 16 if (is_dir($path . "/" . $filename)) {// 如果讀取的某個(gè)對(duì)象是文件夾,則遞歸 17 addFileToZip($path . "/" . $filename, $zip); 18 } else { //將文件加入zip對(duì)象 19 $zip->addFile($path . "/" . $filename); 20 } 21 } 22 } 23 @closedir($path); 24 } 25 //運(yùn)用 26 $zip = new ZipArchive(); 27 if ($zip->open('test.zip', ZipArchive::OVERWRITE) === TRUE) { 28 addFileToZip('./testdir', $zip); //調(diào)用方法,對(duì)要打包的根目錄進(jìn)行操作,并將ZipArchive的對(duì)象傳遞給方法 29 $zip->close(); //關(guān)閉處理的zip文件 30 }?
?6.打印兩個(gè)日期之間的日期
View Code 1 function prDates($start, $end) { 2 //將ISO Date 轉(zhuǎn)成 Timestamp 3 $dt_start = strtotime($start); 4 $dt_end = strtotime($end); 5 do { 6 //將 Timestamp 轉(zhuǎn)成 ISO Date 輸出 7 echo date('Y-m-d', $dt_start).PHP_EOL; 8 } while (($dt_start += 86400) <= $dt_end); // 重復(fù) Timestamp + 1 天(86400), 直至大于結(jié)束日期中止 9 }?
?7.RC4加密算法
View Code 1 /* 2 * rc4加密算法,二次加密即可還原 3 * $pwd 密鑰 4 * $data 要加密的數(shù)據(jù) 5 */ 6 function rc4 ($pwd, $data)//$pwd密鑰 $data需加密字符串 7 { 8 $key[] =""; 9 $box[] =""; 10 11 $pwd_length = strlen($pwd); 12 $data_length = strlen($data); 13 14 for ($i = 0; $i < 256; $i++) 15 { 16 $key[$i] = ord($pwd[$i % $pwd_length]); 17 $box[$i] = $i; 18 } 19 20 for ($j = $i = 0; $i < 256; $i++) 21 { 22 $j = ($j + $box[$i] + $key[$i]) % 256; 23 $tmp = $box[$i]; 24 $box[$i] = $box[$j]; 25 $box[$j] = $tmp; 26 } 27 28 for ($a = $j = $i = 0; $i < $data_length; $i++) 29 { 30 $a = ($a + 1) % 256; 31 $j = ($j + $box[$a]) % 256; 32 33 $tmp = $box[$a]; 34 $box[$a] = $box[$j]; 35 $box[$j] = $tmp; 36 37 $k = $box[(($box[$a] + $box[$j]) % 256)]; 38 $cipher .= chr(ord($data[$i]) ^ $k); 39 } 40 41 return $cipher; 42 }?
?8.utf8_substr UTF8編碼字符串截取,解決中英文混雜截取
View Code 1 /** 2 * $sting 輸入字符串 3 * $start 截取起始位,默認(rèn)為0 4 * $length 截取長(zhǎng)度,中英文長(zhǎng)度都為1,默認(rèn)為'',跟輸入負(fù)數(shù)一樣截取至字符串結(jié)束。 5 * [\x00-\x7F] 匹配英文字符 6 * [\xC0-\xFF][\x80-\xBF]+ 匹配中文字符 7 * 模式修正符s 將換行當(dāng)成普通字符,此時(shí).*能匹配包括換行任意字符 8 */ 9 function utf8_substr($string, $start=0, $length=''){ 10 return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$start.'}' 11 .'((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$length.'}).*#s','$1',$string); 12 }?
9.創(chuàng)建透明圖片
$image = imagecreatetruecolor(300,300); $transparent = imagecolorallocatealpha($image, 255, 255, 255,127); //創(chuàng)建透明背景色 imagecolortransparent($img, $transparent); imagefill($image, 0, 0, $transparent); //填充透明背景色 View Code?
?10.圖片灰白化
1 <?php 2 /** 3 * 圖片黑白處理 4 * @params $file 圖片地址 5 * @params $output 直接輸出 6 * @params $newFile 保存黑白圖片 7 * @usage 8 1、imageGray('test.png'); 9 2、imageGray('test.png',false,'test_gray.png'); 10 */ 11 function imageGray($file,$output=true,$newFile=false){ 12 if(preg_match('#\.(jpg|png|jpeg|gif)$#i',$file,$match)){ 13 //不同類(lèi)型圖片讀取函數(shù) 14 $create_function = 'imagecreatefrom'.$match[1]; 15 //根據(jù)不同類(lèi)型圖片創(chuàng)建圖片 16 $im = $create_function($file); 17 18 //過(guò)濾成灰色圖片 19 if($im) imagefilter($im, IMG_FILTER_GRAYSCALE); 20 21 //不同類(lèi)型圖片寫(xiě)入函數(shù) 22 $image_function = 'image'.$match[1]; 23 //輸出到瀏覽器端 24 if ($output) { 25 header ("Content-type: image/".$match[1]); 26 $image_function($im); 27 } 28 //保存黑白照片 29 if($newFile) { 30 $file = $newFile; 31 $image_function($im,$file); 32 } 33 //刪除圖片資源 34 imagedestroy($im); 35 } 36 return $file; 37 } 38 ?> View Code?
?11.域名授權(quán)認(rèn)證
1 function checkAuthenticated() 2 { 3 //合法域名數(shù)組 4 $domain_array = array( 5 base64_encode(base64_encode('127.0.0.1')), 6 base64_encode(base64_encode('localhost')), 7 base64_encode(base64_encode('test.com')), 8 base64_encode(base64_encode('*.test.com')), 9 ); 10 //對(duì)域名數(shù)組系列化并進(jìn)行base64編碼,為了安全,做了拼接處理,防止被直接解編碼反系列化獲取結(jié)果 11 $str = base64_encode(base64_encode(serialize($domain_array))."|".serialize($domain_array)); 12 //解編碼,分離拼接 13 $arr = explode("|",base64_decode($str)); 14 //獲取數(shù)據(jù)系列化部分,反系列化得到域名數(shù)組 15 $arr = unserialize($arr[1]); 16 //遍歷域名數(shù)組,解編碼獲取域名 17 foreach($arr as $k=>$v) 18 { 19 $arr[$k] = base64_decode(base64_decode($v)); 20 } 21 //獲取當(dāng)前URL的host地址 22 $host = $_SERVER['HTTP_HOST']; 23 //host:端口 分離 24 $host = explode(":",$host); 25 //去除端口部分 26 $host = $host[0]; 27 //認(rèn)證通過(guò)標(biāo)志,默認(rèn)未通過(guò)認(rèn)證 28 $passed = false; 29 //遍歷域名數(shù)組 30 foreach($arr as $k=>$v) 31 { 32 //帶通配子域名情況,使用preg_match進(jìn)行匹配檢測(cè) 33 if(substr($v,0,2)=='*.') 34 { 35 $preg_str = substr($v,2); //去掉通配部分 36 if(preg_match("/".$preg_str."$/",$host)>0) //preg_match('/test.com$/','www.test.com') ,$preg_str中的.可匹配任意字符,包括. 37 { 38 $passed = true; //匹配通過(guò),退出循環(huán) 39 break; 40 } 41 } 42 } 43 if(!$passed) //標(biāo)志為假,因?yàn)閒oreach中為判斷非通配域名(test.com),因此還需要進(jìn)行一次in_array判斷 44 { 45 if(!in_array($host,$arr)) 46 { 47 return false; 48 } 49 } 50 return true; 51 } View Code?
12.獲取文件權(quán)限值
1 function getChmod($filepath){ 2 return substr(base_convert(@fileperms($filepath),10,8),-4); 3 }?
13.數(shù)組不定長(zhǎng)多鍵值排序
1 <?php 2 /** 3 * PHP數(shù)組不定長(zhǎng)多鍵值排序 4 * 5 * @param array $list 數(shù)據(jù)源 6 * @param array $rules 排序規(guī)則 ['key1'=>'asc', 'key2' => 'desc', ...] 7 * @return array 8 */ 9 function smartMultiSort($list, $rules) { 10 $multisortParams = []; 11 foreach($rules as $key => $sort) { 12 $multisortParams[$key] = []; 13 $multisortParams[$key . $sort] = constant(strtoupper("sort_{$sort}")); 14 } 15 foreach($list as $item) { 16 foreach($rules as $key => $sort) { 17 $multisortParams[$key][] = $item[$key]; 18 } 19 } 20 $multisortParams[] = &$list; 21 22 call_user_func_array('array_multisort', $multisortParams); 23 return array_pop($multisortParams); 24 } 25 26 //示例 27 $list = array (); 28 $list [] = array ( 29 'id' => 1, 30 'name' => '學(xué)生1', 31 'school' => '學(xué)校1', 32 'class' => '班級(jí)1' 33 ); 34 $list [] = array ( 35 'id' => 4, 36 'name' => '學(xué)生4', 37 'school' => '學(xué)校2', 38 'class' => '班級(jí)2' 39 ); 40 $list [] = array ( 41 'id' => 3, 42 'name' => '學(xué)生3', 43 'school' => '學(xué)校2', 44 'class' => '班級(jí)1' 45 ); 46 $list [] = array ( 47 'id' => 2, 48 'name' => '學(xué)生2', 49 'school' => '學(xué)校1', 50 'class' => '班級(jí)2' 51 ); 52 $list [] = array ( 53 'id' => 5, 54 'name' => '學(xué)生5', 55 'school' => '學(xué)校2', 56 'class' => '班級(jí)3' 57 ); 58 print_r(smartMultiSort($list, ['school' => 'desc','id' => 'asc'])); View Code?
總結(jié)
- 上一篇: 梦到前妻生病预示什么意思
- 下一篇: PHP autoload实践