浏览 3129 次
锁定老帖子 主题:common.inc.php
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-10-28
最后修改:2011-06-27
<?php /* * 部分函数取自DISCUZ,dedecms * */ define ( 'DISCUZ_ROOT', substr ( dirname ( __FILE__ ), 0, - 12 ) ); define ( 'MAGIC_QUOTES_GPC', function_exists ( 'get_magic_quotes_gpc' ) && get_magic_quotes_gpc () ); define ( 'ICONV_ENABLE', function_exists ( 'iconv' ) ); define ( 'MB_ENABLE', function_exists ( 'mb_convert_encoding' ) ); define ( 'EXT_OBGZIP', function_exists ( 'ob_gzhandler' ) ); //切分数组参数 $skip 为跳过的item function implodearray($array, $skip = array()) { $return = ''; if (is_array ( $array ) && ! empty ( $array )) { foreach ( $array as $key => $value ) { if (empty ( $skip ) || ! in_array ( $key, $skip )) { if (is_array ( $value )) { $return .= "$key={" . implodearray ( $value, $skip ) . "}; "; } else { $return .= "$key=$value; "; } } } } return $return; } //用法 $extralog = implodearray ( array ('GET' => $_GET, 'POST' => $_POST ), array ('formhash', 'submit', 'addsubmit', 'admin_password', 'sid', 'action' ) ); $mime_types = array ('gif' => 'image/gif', 'jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'jpe' => 'image/jpeg', 'bmp' => 'image/bmp', 'png' => 'image/png', 'tif' => 'image/tiff', 'tiff' => 'image/tiff', 'pict' => 'image/x-pict', 'pic' => 'image/x-pict', 'pct' => 'image/x-pict', 'tif' => 'image/tiff', 'tiff' => 'image/tiff', 'psd' => 'image/x-photoshop', 'swf' => 'application/x-shockwave-flash', 'js' => 'application/x-javascrīpt', 'pdf' => 'application/pdf', 'ps' => 'application/postscrīpt', 'eps' => 'application/postscrīpt', 'ai' => 'application/postscrīpt', 'wmf' => 'application/x-msmetafile', 'css' => 'text/css', 'htm' => 'text/html', 'html' => 'text/html', 'txt' => 'text/plain', 'xml' => 'text/xml', 'wml' => 'text/wml', 'wbmp' => 'image/vnd.wap.wbmp', 'mid' => 'audio/midi', 'wav' => 'audio/wav', 'mp3' => 'audio/mpeg', 'mp2' => 'audio/mpeg', 'avi' => 'video/x-msvideo', 'mpeg' => 'video/mpeg', 'mpg' => 'video/mpeg', 'qt' => 'video/quicktime', 'mov' => 'video/quicktime', 'lha' => 'application/x-lha', 'lzh' => 'application/x-lha', 'z' => 'application/x-compress', 'gtar' => 'application/x-gtar', 'gz' => 'application/x-gzip', 'gzip' => 'application/x-gzip', 'tgz' => 'application/x-gzip', 'tar' => 'application/x-tar', 'bz2' => 'application/bzip2', 'zip' => 'application/zip', 'arj' => 'application/x-arj', 'rar' => 'application/x-rar-compressed', 'hqx' => 'application/mac-binhex40', 'sit' => 'application/x-stuffit', 'bin' => 'application/x-macbinary', 'uu' => 'text/x-uuencode', 'uue' => 'text/x-uuencode', 'latex' => 'application/x-latex', 'ltx' => 'application/x-latex', 'tcl' => 'application/x-tcl', 'pgp' => 'application/pgp', 'asc' => 'application/pgp', 'exe' => 'application/x-msdownload', 'doc' => 'application/msword', 'rtf' => 'application/rtf', 'xls' => 'application/vnd.ms-excel', 'ppt' => 'application/vnd.ms-powerpoint', 'mdb' => 'application/x-msaccess', 'wri' => 'application/x-mswrite' ); //读取文件函数 function readfromfile($file_name) { if (file_exists ( $file_name )) { $filenum = fopen ( $file_name, "r" ); flock ( $filenum, LOCK_EX ); $file_data = fread ( $filenum, filesize ( $file_name ) ); rewind ( $filenum ); fclose ( $filenum ); return $file_data; } } //写入文件函数 function writetofile($file_name, $data, $method = "w") { $filenum = fopen ( $file_name, $method ); flock ( $filenum, LOCK_EX ); $file_data = fwrite ( $filenum, $data ); fclose ( $filenum ); return $file_data; } /** * 将一个字串中含有全角的数字字符、字母、空格或'%+-()'字符转换为相应半角字符 * * @access public * @param string $str 待转换字串 * * @return string $str 处理后字串 */ function make_semiangle($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); } /** * 获得用户操作系统的换行符 * * @access public * @return string */ function get_crlf() { /* LF (Line Feed, 0x0A, \N) 和 CR(Carriage Return, 0x0D, \R) */ if (stristr($_SERVER['HTTP_USER_AGENT'], 'Win')) { $the_crlf = '\r\n'; } elseif (stristr($_SERVER['HTTP_USER_AGENT'], 'Mac')) { $the_crlf = '\r'; // for old MAC OS } else { $the_crlf = '\n'; } return $the_crlf; } //产生随机字符串函数 function random($length) { $hash = ''; $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz'; $max = strlen ( $chars ) - 1; mt_srand ( ( double ) microtime () * 1000000 ); for($i = 0; $i < $length; $i ++) { $hash .= $chars [mt_rand ( 0, $max )]; } return $hash; } //HTML表格的每行转为CSV格式数组 function get_tr_array($table) { $table = preg_replace ( "'<td[^>]*?>'si", '"', $table ); $table = str_replace ( "</td>", '",', $table ); $table = str_replace ( "</tr>", "{tr}", $table ); //去掉 HTML 标记 $table = preg_replace ( "'<[\/\!]*?[^<>]*?>'si", "", $table ); //去掉空白字符 $table = preg_replace ( "'([\r\n])[\s]+'", "", $table ); $table = str_replace ( " ", "", $table ); $table = str_replace ( " ", "", $table ); $table = explode ( ",{tr}", $table ); array_pop ( $table ); return $table; } //将HTML表格的每行每列转为数组,采集表格数据 function get_td_array($table) { $table = preg_replace ( "'<table[^>]*?>'si", "", $table ); $table = preg_replace ( "'<tr[^>]*?>'si", "", $table ); $table = preg_replace ( "'<td[^>]*?>'si", "", $table ); $table = str_replace ( "</tr>", "{tr}", $table ); $table = str_replace ( "</td>", "{td}", $table ); //去掉 HTML 标记 $table = preg_replace ( "'<[\/\!]*?[^<>]*?>'si", "", $table ); //去掉空白字符 $table = preg_replace ( "'([\r\n])[\s]+'", "", $table ); $table = str_replace ( " ", "", $table ); $table = str_replace ( " ", "", $table ); $table = explode ( '{tr}', $table ); array_pop ( $table ); foreach ( $table as $key => $tr ) { $td = explode ( '{td}', $tr ); array_pop ( $td ); $td_array [] = $td; } return $td_array; } //返回字符串中的所有单词 $distinct=true 去除重复 function split_en_str($str, $distinct = true) { preg_match_all ( '/([a-zA-Z]+)/', $str, $match ); if ($distinct == true) { $match [1] = array_unique ( $match [1] ); } sort ( $match [1] ); return $match [1]; } //服务器IP function get_server_ip() { if (isset ( $_SERVER )) { if ($_SERVER ['SERVER_ADDR']) $huoqu_ip = $_SERVER ['SERVER_ADDR']; else $huoqu_ip = $_SERVER ['LOCAL_ADDR']; } else { $huoqu_ip = getenv ( 'SERVER_ADDR' ); } return $huoqu_ip; } // $rptype = 0 表示仅替换 html标记 // $rptype = 1 表示替换 html标记同时去除连续空白字符 // $rptype = 2 表示替换 html标记同时去除所有空白字符 // $rptype = -1 表示仅替换 html危险的标记 function HtmlReplace($str, $rptype = 0) { $str = stripslashes ( $str ); if ($rptype == 0) { $str = htmlspecialchars ( $str ); } else if ($rptype == 1) { $str = htmlspecialchars ( $str ); $str = str_replace ( " ", ' ', $str ); $str = ereg_replace ( "[\r\n\t ]{1,}", ' ', $str ); } else if ($rptype == 2) { $str = htmlspecialchars ( $str ); $str = str_replace ( " ", '', $str ); $str = ereg_replace ( "[\r\n\t ]", '', $str ); } else { $str = ereg_replace ( "[\r\n\t ]{1,}", ' ', $str ); $str = eregi_replace ( 'script', 'script', $str ); $str = eregi_replace ( "<[/]{0,1}(link|meta|ifr|fra)[^>]*>", '', $str ); } return addslashes ( $str ); } function ParCv($n) { return chr ( $n ); } // 取得文件扩展名 function fileext($filename) { return addslashes ( trim ( substr ( strrchr ( $filename, '.' ), 1, 10 ) ) ); } function strexists($string, $find) { return ! (strpos ( $string, $find ) === FALSE); } //递归ddslashes function daddslashes($string, $force = 0, $strip = FALSE) { if (! MAGIC_QUOTES_GPC || $force) { if (is_array ( $string )) { foreach ( $string as $key => $val ) { $string [$key] = daddslashes ( $val, $force ); } } else { $string = addslashes ( $strip ? stripslashes ( $string ) : $string ); } } return $string; } //递归stripslashes function dstripslashes($string) { if (is_array ( $string )) { foreach ( $string as $key => $val ) { $string [$key] = $this->dstripslashes ( $val ); } } else { $string = stripslashes ( $string ); } return $string; } //提取页面和浏览器提交的变量,作用相当于使PHP.INI开了全局变量 @extract ( $_SERVER, EXTR_SKIP ); @extract ( $_SESSION, EXTR_SKIP ); @extract ( $_POST, EXTR_SKIP ); @extract ( $_FILES, EXTR_SKIP ); @extract ( $_GET, EXTR_SKIP ); @extract ( $_ENV, EXTR_SKIP ); function getgpc($k, $t = 'R') { switch ($t) { case 'P' : $var = &$_POST; break; case 'G' : $var = &$_GET; break; case 'C' : $var = &$_COOKIE; break; case 'R' : $var = &$_REQUEST; break; } return isset ( $var [$k] ) ? (is_array ( $var [$k] ) ? $var [$k] : trim ( $var [$k] )) : NULL; } //获得当前的脚本网址 function GetCurUrl() { if (! empty ( $_SERVER ["REQUEST_URI"] )) { $scriptName = $_SERVER ["REQUEST_URI"]; $nowurl = $scriptName; } else { $scriptName = $_SERVER ["PHP_SELF"]; if (empty ( $_SERVER ["QUERY_STRING"] )) { $nowurl = $scriptName; } else { $nowurl = $scriptName . "?" . $_SERVER ["QUERY_STRING"]; } } return $nowurl; } /* * 相对路径转化成绝对路径 * $content:网页内容; * $feed_url:网站域名; * */ function relative_to_absolute($content, $feed_url) { preg_match ( '/(http|https|ftp):///', $feed_url, $protocol ); $server_url = preg_replace ( "/(http|https|ftp|news):///", "", $feed_url ); $server_url = preg_replace ( "//.*/", "", $server_url ); if ($server_url == '') { return $content; } if (isset ( $protocol [0] )) { $new_content = preg_replace ( '/href="//', 'href="' . $protocol [0] . $server_url . '/', $content ); $new_content = preg_replace ( '/src="//', 'src="' . $protocol [0] . $server_url . '/', $new_content ); } else { $new_content = $content; } return $new_content; } //取得所有链接 function get_all_url($code) { preg_match_all ( '/<a\s+href=["|\']?([^>"\' ]+)["|\']?\s*[^>]*>([^>]+)<\/a>/i', $code, $arr ); return array ('name' => $arr [2], 'url' => $arr [1] ); } //文本转HTML function Text2Html($txt) { $txt = str_replace ( " ", " ", $txt ); $txt = str_replace ( "<", "<", $txt ); $txt = str_replace ( ">", ">", $txt ); $txt = preg_replace ( "/[\r\n]{1,}/isU", "<br/>\r\n", $txt ); return $txt; } function AttDef($oldvar, $nv) { return empty ( $oldvar ) ? $nv : $oldvar; } //处理禁用HTML但允许换行的内容 function TrimMsg($msg) { $msg = trim ( stripslashes ( $msg ) ); $msg = nl2br ( htmlspecialchars ( $msg ) ); $msg = str_replace ( " ", " ", $msg ); return addslashes ( $msg ); } function jstrim($str, $len) { $str = preg_replace ( "/{quote}(.*){\/quote}/is", '', $str ); $str = str_replace ( '<br/>', ' ', $str ); $str = cn_substr ( $str, $len ); $str = ereg_replace ( "['\"\r\n]", "", $str ); return $str; } function jstrimjajx($str, $len) { $str = preg_replace ( "/{quote}(.*){\/quote}/is", '', $str ); $str = str_replace ( '<br/>', ' ', $str ); $str = cn_substr ( $str, $len ); $str = ereg_replace ( "['\"\r\n]", "", $str ); $str = str_replace ( '<', '<', $str ); $str = str_replace ( '>', '>', $str ); return $str; } /* * 功能:格式化数字,以标准MONEY格式输出 */ function formatNumber($num) { return number_format ( $num, 2, ".", "," ); } /* * 功能:格式化文本,将\n转成<br>等 * 参数:$string 来源字符串 * 返回:处理后的字符串 */ function formatString($string = "") { $string = preg_replace ( array ("/ /", "/ /" ), array (" ", " " ), $string ); return nl2br ( $string ); } /* * 功能:格式化文本输出 * 参数 $text 为需格式化的文本内容 */ function formatContent($text) { $trans = get_html_translation_table ( HTML_SPECIALCHARS ); $trans = array_flip ( $trans ); $text = strtr ( $text, $trans ); //$text = str_replace("\n", "<br>", $text); //$text = str_replace(" ", " ", $text); return $text; } /* * 将字节转换成Kb或者Mb... * 参数 $num为字节大小 */ function bitSize($num) { if (! preg_match ( "/^[0-9]+$/", $num )) return 0; $type = array ("B", "KB", "MB", "GB", "TB", "PB" ); $j = 0; while ( $num >= 1024 ) { if ($j >= 5) return $num . $type [$j]; $num = $num / 1024; $j ++; } return $num . $type [$j]; } ?> 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |