您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 信息化管理 > php开发中常用函数集合
?php/*****************************************************************--函数名:set_size--作用:得到文件的大小--参数:$filename文件名--返回值:字符串--实例:echoset_size(ll0617.rar);*******************************************************************/functionset_size($filename){if(is_file($filename)){$size=filesize($filename);settype($size,string);for($aaa=strlen($size)-1;$aaa=0;$aaa--){$dx=$dx.$size{$aaa};}$size=$dx;for($i=0;$i=floor((strlen($size)-1)/3);$i++){$i==0?$begin=0:$begin=$i*3;//b=$begin;$end=$begin+2;for($begin;$begin=$end;$begin++){if($i==floor((strlen($size)-1)/3)){$res=$res.$size{$begin};}else{$begin!=$end?$res=$res.$size{$begin}:$res=$res.$size{$begin}.,;}}}unset($dx);settype($res,string);for($aaa=strlen($res)-1;$aaa=0;$aaa--){$dx=$dx.$res{$aaa};}return$dx;//unset($dx);}else{returnfalse;}}/*****************************************************************--函数名:w_or_r--作用:文件读写权限--参数:$type,$filename--返回值:布尔--实例:echow_or_r(r,ll0617.rar);*******************************************************************/functionw_or_r($type,$filename){if(file_exists($filename)){if($type==w){if(is_readable($filename)){return可读;}else{return不可读;}}elseif($type==r){if(is_writable($filename)){$path=/txt|asp|php|ini|html|htm|temp/i;$path_parts=pathinfo($filename);$string=$path_parts[extension];if(preg_match_all($path,$string,$re)){returnAhref=\#\onclick=\javascript:Wopen=open('read.php?url=$path_parts[basename]','','width=600,height=600');\编辑/a;}else{return可写;}}else{return不可写;}}else{returnfalse;}}}/*****************************************************************--函数名:getPageContent和replaceHtmlAndJs(必须)--作用:php获取网页标题和内容函数(不包含html标签),可用作类似chinaz的Meta信息检测--参数:$url所要得到的网址--返回值:数组--实例:$a=getPageContent();print_r($a);*******************************************************************/functiongetPageContent($url){$pageinfo=array();$pageinfo[content_type]='';$pageinfo[charset]='';$pageinfo[title]='';$pageinfo[description]='';$pageinfo[keywords]='';$pageinfo[body]='';$pageinfo['httpcode']=200;$pageinfo['all']='';$ch=curl_init();curl_setopt($ch,CURLOPT_USERAGENT,Mozilla/4.0(compatible;MSIE5.01;WindowsNT5.0));curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);curl_setopt($ch,CURLOPT_TIMEOUT,8);curl_setopt($ch,CURLOPT_FILETIME,1);curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);curl_setopt($ch,CURLOPT_HEADER,1);curl_setopt($ch,CURLOPT_URL,$url);$curl_start=microtime(true);$store=curl_exec($ch);$curl_time=microtime(true)-$curl_start;if(curl_error($ch)){$pageinfo['httpcode']=505;//gatewayerrorecho'Curlerror:'.curl_error($ch).\n;return$pageinfo;}//print_r(curl_getinfo($ch));$pageinfo['httpcode']=curl_getinfo($ch,CURLINFO_HTTP_CODE);//echocurl_getinfo($ch,CURLINFO_CONTENT_TYPE).\n;$pageinfo[content_type]=curl_getinfo($ch,CURLINFO_CONTENT_TYPE);if(intval($pageinfo['httpcode'])200or!preg_match('@text/html@',curl_getinfo($ch,CURLINFO_CONTENT_TYPE))){//print_r(curl_getinfo($ch));//exit;return$pageinfo;}preg_match('/charset=([^\s\n\r]+)/i',curl_getinfo($ch,CURLINFO_CONTENT_TYPE),$matches);//从header里取charsetif(trim($matches[1])){$pageinfo[charset]=trim($matches[1]);}//echo$pageinfo[charset];//exit;curl_close($ch);//echo$store;//removejavascript$store=preg_replace(/script.*(.*)\/script/smUi,'',$store);//removelink$store=preg_replace(/link\s+[^]+/smUi,'',$store);//remove!----$store=preg_replace(/!--.*--/smUi,'',$store);//removestyle/style$store=preg_replace(/style.*(.*)\/style/smUi,'',$store);//remove中文空格$store=preg_replace(//,'',$store);//remove标点符号//$store=preg_replace(/[\~`!@#$%^&*()_\-+={}|\[\]\\;':\\\?\,\.\/]/,'',$store);//preg_match(/head.*(.*)\/head/smUi,$store,$matches);//$head=$matches[1];//echo$head.\n;//charsetif($pageinfo[charset]==''){preg_match('@meta.+charset=([\w\-]+)[^]*@i',$store,$matches);$pageinfo[charset]=trim($matches[1]);}//desctiptionpreg_match('@meta\s+name=\*description\*\s+content\s*=\s*([^/]+)/*@i',$store,$matches);//print_r($matches);$desc=trim($matches[1]);$pageinfo[description]=str_replace(\,'',$desc);preg_match('@meta\s+name=\*keywords\*\s+content\s*=\s*([^/]+)/*@i',$store,$matches);//print_r($matches);$keywords=trim($matches[1]);$pageinfo[keywords]=str_replace(\,'',$keywords);preg_match(/title(.*)\/title/smUi,$store,$matches);$pageinfo[title]=trim($matches[1]);preg_match(/body.*(.*)\/body/smUi,$store,$matches);$pageinfo[body]=addslashes(replaceHtmlAndJs($matches[1]));$pageinfo['all']=addslashes(replaceHtmlAndJs($store));//echocharset=.$pageinfo[charset].\n;//print_r($pageinfo);//exit;return$pageinfo;}/***去掉所有的HTML标记和JavaScript标记*/functionreplaceHtmlAndJs($document){$document=trim($document);if(strlen($document)=0){return$document;}$search=array('script[^]*?.*?/script'si,//去掉javascript'[\/\!]*?[^]*?'si,//去掉HTML标记'[\r\n\s+]',//去掉空白字符'&(\w+);'i//替换HTML实体);//作为PHP代码运行$replace=array(,,,);return@preg_replace($search,$replace,$document);}/*************************************************************
本文标题:php开发中常用函数集合
链接地址:https://www.777doc.com/doc-5099158 .html