1.获取页面闭合带id标签数据
View Code
1 $pre_div){31 $divs[(int)$pre_div[1]] = 'p';32 $divs[(int)$suf_matches[0][$index][1]] = 's'; 33 }34 35 //对div进行排序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 //';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_php
1.1 由(1)改进为获取页面任意标签,参考《》
View Code
1 格式,是则添加结束标签,否则为false; 注:img、input等可能不是这种格式,此时$suf_matches[0]为空。 39 if(!empty($suf_matches[0])) $endTag = ' '; 40 else $endTag = false; 41 42 //合并所有HTML标签 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 //非 格式,获取前缀下标后的第一个>作为标签结束 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 //对所有HTML标签按index进行排序 59 $sort = array_keys($htmltags); 60 asort($sort); 61 62 //开始获取命中字符串 63 $hitTagStrings = array(); 64 foreach($hits[0] as $hit){ 65 $hit = $hit[1]; //获取命中index 66 67 $count = count($sort); //循环控制,$count--避免无限循环 68 foreach($pre_matches[0] as $index=>$pre_div){ 69 //最后一个$pre_matches[0][$index+1]会造成数组出界,因此设置其index等于总长度 70 if(!isset($pre_matches[0][$index+1][1])) $pre_matches[0][$index+1][1] = strlen($data); 71 72 //$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数据至远程服务器并获取返回数据
View Code
1 '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.文件夹复制
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"; 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 * 遍历目录并获取所有目录即文件,以数组array('dirs'=>$dirs,'files'=>$files)方式返回。 3 * @param $dir //搜索目录 4 * @param $pattern // '*'搜索全部文件,可以智能匹配,如*.jpg 搜索jpg文件,*.{jpg,png}搜索jpg和png文件,区分大小写!! 5 * @param $skip //排除遍历文件,如"*.{jpg,png}"排除.jpg和.png类型文件 6 * @param $subInclude //默认遍历子目录,$subInclude设置为false则仅遍历当前目录 7 * @param @flag //glob函数的标记,有效标记如下: 8 GLOB_MARK - 在每个返回的项目中加一个斜线 9 GLOB_NOSORT - 按照文件在目录中出现的原始顺序返回(不排序) 10 GLOB_NOCHECK - 如果没有文件匹配则返回用于搜索的模式 11 GLOB_NOESCAPE - 反斜线不转义元字符 12 GLOB_BRACE - 扩充 {a,b,c} 来匹配 'a','b' 或 'c' 13 GLOB_ONLYDIR - 仅返回与模式匹配的目录项 14 */15 function scandir_through($dir,$pattern='*',$skip=false,$subInclude=true,$flag=GLOB_BRACE){16 $dirs = array();17 $files = array();18 //获取当前目录下所有文件及文件夹19 $items = glob($dir . '/*');20 21 //遍历所有项目,若设置$subInclude为true,则继续遍历子目录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 //若当前文件匹配文件查找模式$pattern,则加入$files数组中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文件函数 3 * @param $path //打包文件夹路径 4 * @param $zip //ZipArchive 对象 5 */ 6 function addFileToZip($path, $zip) { 7 $handler = opendir($path); //打开当前文件夹由$path指定。 8 /* 9 循环的读取文件夹下的所有文件和文件夹10 其中$filename = readdir($handler)是每次循环的时候将读取的文件名赋值给$filename,11 为了不陷于死循环,所以还要让$filename !== false。12 一定要用!==,因为如果某个文件名如果叫'0',或者某些被系统认为是代表false,用!=就会停止循环13 */14 while (($filename = readdir($handler)) !== false) {15 if ($filename != "." && $filename != "..") { //文件夹文件名字为'.'和‘..’,不要对他们进行操作16 if (is_dir($path . "/" . $filename)) { // 如果读取的某个对象是文件夹,则递归17 addFileToZip($path . "/" . $filename, $zip);18 } else { //将文件加入zip对象19 $zip->addFile($path . "/" . $filename);20 }21 }22 }23 @closedir($path);24 }25 //运用26 $zip = new ZipArchive();27 if ($zip->open('test.zip', ZipArchive::OVERWRITE) === TRUE) {28 addFileToZip('./testdir', $zip); //调用方法,对要打包的根目录进行操作,并将ZipArchive的对象传递给方法29 $zip->close(); //关闭处理的zip文件30 }
6.打印两个日期之间的日期
View Code
1 function prDates($start, $end) { 2 //将ISO Date 转成 Timestamp3 $dt_start = strtotime($start);4 $dt_end = strtotime($end);5 do { 6 //将 Timestamp 转成 ISO Date 输出7 echo date('Y-m-d', $dt_start).PHP_EOL;8 } while (($dt_start += 86400) <= $dt_end); // 重复 Timestamp + 1 天(86400), 直至大于结束日期中止9 }
7.RC4加密算法
View Code
1 /* 2 * rc4加密算法,二次加密即可还原 3 * $pwd 密钥 4 * $data 要加密的数据 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 截取起始位,默认为0 4 * $length 截取长度,中英文长度都为1,默认为'',跟输入负数一样截取至字符串结束。 5 * [\x00-\x7F] 匹配英文字符 6 * [\xC0-\xFF][\x80-\xBF]+ 匹配中文字符 7 * 模式修正符s 将换行当成普通字符,此时.*能匹配包括换行任意字符 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.创建透明图片
$image = imagecreatetruecolor(300,300); $transparent = imagecolorallocatealpha($image, 255, 255, 255,127); //创建透明背景色 imagecolortransparent($img, $transparent);imagefill($image, 0, 0, $transparent); //填充透明背景色
10.图片灰白化
1
11.域名授权认证
1 function checkAuthenticated() 2 { 3 //合法域名数组 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 //对域名数组系列化并进行base64编码,为了安全,做了拼接处理,防止被直接解编码反系列化获取结果11 $str = base64_encode(base64_encode(serialize($domain_array))."|".serialize($domain_array));12 //解编码,分离拼接13 $arr = explode("|",base64_decode($str));14 //获取数据系列化部分,反系列化得到域名数组15 $arr = unserialize($arr[1]);16 //遍历域名数组,解编码获取域名17 foreach($arr as $k=>$v)18 {19 $arr[$k] = base64_decode(base64_decode($v));20 }21 //获取当前URL的host地址22 $host = $_SERVER['HTTP_HOST'];23 //host:端口 分离24 $host = explode(":",$host);25 //去除端口部分26 $host = $host[0];27 //认证通过标志,默认未通过认证28 $passed = false;29 //遍历域名数组30 foreach($arr as $k=>$v)31 {32 //带通配子域名情况,使用preg_match进行匹配检测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; //匹配通过,退出循环39 break;40 }41 }42 }43 if(!$passed) //标志为假,因为foreach中为判断非通配域名(test.com),因此还需要进行一次in_array判断44 {45 if(!in_array($host,$arr))46 {47 return false;48 }49 }50 return true;51 }
12.获取文件权限值
1 function getChmod($filepath){2 return substr(base_convert(@fileperms($filepath),10,8),-4);3 }
13.数组不定长多键值排序
1 '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' => '学生1',31 'school' => '学校1',32 'class' => '班级1'33 );34 $list [] = array (35 'id' => 4,36 'name' => '学生4',37 'school' => '学校2',38 'class' => '班级2'39 );40 $list [] = array (41 'id' => 3,42 'name' => '学生3',43 'school' => '学校2',44 'class' => '班级1'45 );46 $list [] = array (47 'id' => 2,48 'name' => '学生2',49 'school' => '学校1',50 'class' => '班级2'51 );52 $list [] = array (53 'id' => 5,54 'name' => '学生5',55 'school' => '学校2',56 'class' => '班级3'57 );58 print_r(smartMultiSort($list, ['school' => 'desc','id' => 'asc']));