常用的common function库
View Code
1 <?php 2 //因为前后台都要用到,所以放在supermario文件夹下方便重用 3 4 /** 5 * 喔~可爱滴小数点~喔~可爱滴千位数~ 6 * 如果位数超过4位,则将第3位前增加, 7 * @param int $gold 8 * @return string 9 */ 10 function showGold($gold) { 11 return number_format($gold); 12 } 13 14 /** 15 * 页面压缩输出 16 */ 17 function ob_callback($buffer) { 18 header('Etag: '.md5($buffer)); 19 if( extension_loaded('zlib') AND strstr($_SERVER["HTTP_ACCEPT_ENCODING"],"gzip") ) { 20 $buffer = gzencode($buffer,9); 21 header('Content-Encoding: gzip'); 22 header('Vary: Accept-Encoding'); 23 //header('Via: www.guangxitravel.cn'); 24 header('Content-Length: '.strlen($buffer)); 25 } 26 return $buffer; 27 } 28 29 /** 30 * 没有提示的情况下跳转页面 31 * @param string $url 32 * @param string $target 33 */ 34 function go($url='', $target='parent') { 35 if ($url != "") { 36 echo('<script language="javascript">window.location="' . $url . '";</script>'); 37 } else { 38 echo('<script language="javascript">window.location=self.location;"</script>'); 39 } 40 } 41 42 /** 43 * 清除缓存,ECHO,然后DIE(可恶的javascript+php……纯文本输出,总会有一些空字符……) 44 * @param string $msg 45 */ 46 function justSay($msg) { 47 ob_clean(); 48 die($msg); 49 } 50 51 /** 52 * 将符合第二个参数的第一个参数里的内容替换为红色 53 * @param string $data 54 * @param array $keyword 55 * @return string 56 */ 57 function change_keyword($data, $keywords) { 58 if (is_array($keywords)) { 59 foreach ($keywords as $k => $v) { 60 $data = str_replace($v, '<span class="red b">' . $v . '</span>', $data); 61 } 62 return $data; 63 } else { 64 return str_replace($keywords, '<span class="red b">' . $keywords . '</span>', $data); 65 } 66 } 67 68 /** 69 * 创建完全随机的颜色 70 * @return string 71 */ 72 function makeColor() { 73 $key = '#'; 74 for ($i = 0; $i < 6; $i++) 75 $key.= rand(0, 9);//生成php随机数 76 return $key; 77 } 78 79 /** 80 * 自动加载模型和第三方功能类 81 * @param string $className 82 */ 83 function autoLoad($className) { 84 if (strstr($_SERVER['REQUEST_URI'], "/operator/") == false) { //服务器不支持SCRIPT_URL 85 if (file_exists(siteRoot . '/client/model/' . $className . '.php')) { 86 require_once siteRoot . '/client/model/' . $className . '.php'; 87 return; 88 } 89 } else { 90 if (file_exists(siteRoot . '/manager/model/' . $className . '.php')) { 91 require_once siteRoot . '/manager/model/' . $className . '.php'; 92 return; 93 } 94 } 95 if (file_exists(siteRoot . '/public/modules/' . $className . '.php')) { 96 require_once siteRoot . '/public/modules/' . $className . '.php'; 97 return; 98 } 99 if (file_exists(siteRoot . '/client/order/model/' . $className . '.php')) { 100 require_once siteRoot . '/client/order/model/' . $className . '.php'; 101 return; 102 } 103 } 104 105 /** 106 * 用javascript弹出一条信息 107 * @param <string> $message 108 */ 109 function message($message='') { 110 echo('<script language="javascript">alert("' . $message . '");</script>'); 111 } 112 113 /** 114 * 模拟strstr()的第三个参数,返回$h中,$n之前的数据 115 * //$h = haystack, $n = needle 116 * @param <string> $h 117 * @param <string> $n 118 * @return <string> 119 */ 120 function strstrb($h, $n) { 121 return array_shift(explode($n, $h, 2)); 122 } 123 124 /** 125 * 显示错误信息,并跳转至$pageurl,含有样式 126 * 127 * @param string $messages 128 * @param string $pageurl 129 * @param int $msc 秒数 130 */ 131 function msg($messages, $pageurl='javascript:history.back();', $msc=5) { 132 echo '<title>System Message</title> 133 <style type="text/css"> 134 *{margin:0;padding:0px} 135 body{background:#fff;color:#333;font:12px Verdana, Tahoma, sans-serif;text-align:center;margin:0 auto;} 136 a{text-decoration:none;color:#29458C} 137 a:hover{text-decoration:underline;color:#f90} 138 #msg{border:1px solid #c5d7ef;text-align:left;margin:10% auto; width:50%} 139 #msgtitle{padding:5px 10px;background:#f0f6fb;border-bottom:1px #c5d7ef solid} 140 #msgtitle h1{font-size:14px;font-weight:bold;padding-left:10px;border-left:3px solid #acb4be;color:#1f3a87} 141 #msgcontent {padding:20px 50px;} 142 #msgcontent li{display:block;padding:5px;list-style:none;} 143 #msgcontent p{text-align:center;margin-top:10px;padding:0} 144 </style> 145 </head> 146 <body> 147 <div id="msg"> 148 <div id="msgtitle"> 149 <h1>Web System Message</h1> 150 </div> 151 <div id="msgcontent"> 152 ', $messages, ' 153 <p><a href="', $pageurl, '">>>>请点此处返回</a></p> 154 </div> 155 </div> 156 <meta http-equiv="refresh" content="' . $msc . ';URL=', $pageurl, '">'; 157 exit(); 158 } 159 160 /** 161 * 检查后面的日期是否大于前面的日期 162 * @param type $datecome 前面的日期 163 * @param type $datego 后面的日期 164 * @return type 165 */ 166 function sub_date($datecome, $datego) { 167 $d1 = strtotime($datecome); 168 $d2 = strtotime($datego); 169 $days = round(($d2 - $d1) / 3600 / 24); 170 $days = $days < 1 ? 0 : $days; 171 return $days; 172 } 173 174 /** 175 * 将$string的$length后边的内容用$dot替换 176 * @param string $string 177 * @param int $length 178 * @param string $dot 179 * @return string 180 */ 181 function cutstr($string, $length, $dot = '...') { 182 @extract($string); 183 if (strlen($string) <= $length) { 184 return $string; 185 } 186 $string = htmlspecialchars($string); 187 $string = str_replace(array('&', '"', '<', '>'), array('&', '"', '<', '>'), $string); 188 $strcut = ''; 189 $n = $tn = $noc = 0; 190 while ($n < strlen($string)) { 191 $t = ord($string[$n]); 192 if ($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) { 193 $tn = 1; 194 $n++; 195 $noc++; 196 } elseif (194 <= $t && $t <= 223) { 197 $tn = 2; 198 $n += 2; 199 $noc += 2; 200 } elseif (224 <= $t && $t < 239) { 201 $tn = 3; 202 $n += 3; 203 $noc += 2; 204 } elseif (240 <= $t && $t <= 247) { 205 $tn = 4; 206 $n += 4; 207 $noc += 2; 208 } elseif (248 <= $t && $t <= 251) { 209 $tn = 5; 210 $n += 5; 211 $noc += 2; 212 } elseif ($t == 252 || $t == 253) { 213 $tn = 6; 214 $n += 6; 215 $noc += 2; 216 } else { 217 $n++; 218 } 219 if ($noc >= $length) { 220 break; 221 } 222 } 223 if ($noc > $length) { 224 $n -= $tn; 225 } 226 $strcut = substr($string, 0, $n); 227 $strcut = str_replace(array('&', '"', '<', '>'), array('&', '"', '<', '>'), $strcut); 228 return $strcut . $dot; 229 } 230 231 /** 232 +---------------------------------------------------------- 233 * 如果 magic_quotes_gpc 为开启状态,则使用此方法使用为特殊符号前增加转移符号 234 +---------------------------------------------------------- 235 * @access public 236 +---------------------------------------------------------- 237 * @param string $value 可以为数组 238 +---------------------------------------------------------- 239 * @return string 240 +---------------------------------------------------------- 241 */ 242 function kaddslashes($value) { 243 return $value = is_array($value) ? array_map('kaddslashes', $value) : addslashes($value); 244 } 245 246 /** 247 +---------------------------------------------------------- 248 * 为特殊符号前去除转移符号 249 +---------------------------------------------------------- 250 * @access public 251 +---------------------------------------------------------- 252 * @param string $value 可以为数组 253 +---------------------------------------------------------- 254 * @return string 255 +---------------------------------------------------------- 256 */ 257 function kstripcslashes($value) { 258 return $value = is_array($value) ? array_map('kstripcslashes', $value) : stripcslashes($value); 259 } 260 261 /** 262 * 将字符串内容html实体化,避免一些非法信息直接执行。如果参数是数组,则递归。 263 * @param string $value 264 * @return array 265 */ 266 function khtmlspecialchars($value) { 267 return is_array($value) ? array_map('khtmlspecialchars', $value) : 268 preg_replace('/&((#(\d{3,5}|x[a-fA-F0-9]{4})|[a-zA-Z][a-z0-9]{2,5});)/', '&\\1', str_replace(array('&', '"', '<', '>'), array('&', '"', '<', '>'), $value)); 269 } 270 271 /** 272 * 将字符串中的html去除,如果参数是数组,则递归。 273 * @param string $value 274 * @return array 275 */ 276 function striptags($value) { 277 return $value = is_array($value) ? array_map('striptags', $value) : strip_tags($value); 278 } 279 280 /** 281 * 检查email的合法性 282 * 283 * @param string $email 284 * @return bool 285 */ 286 function check_email($email) { 287 if (preg_match("/([a-z0-9]*[-_\.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z]{2,3}([\.][a-z]{2})?/i", $email)) { 288 return true; 289 } else { 290 return false; 291 } 292 } 293 294 /** 295 * 安全电子邮件地址 296 * 297 * @param string $email 298 * @param string $title 299 * @param <type> $attributes 300 * @return <type> 301 */ 302 function safe_mailto($email, $title = '', $attributes = '') { 303 if (is_array($email)) { 304 $tmp = $email; 305 unset($email); 306 extract($tmp); 307 } 308 $title = (string) $title; 309 310 if ($title == "") { 311 $title = $email; 312 } 313 314 for ($i = 0; $i < 16; $i++) { 315 $x[] = substr('<a href="mailto:', $i, 1); 316 } 317 318 for ($i = 0; $i < strlen($email); $i++) { 319 $x[] = "|" . ord(substr($email, $i, 1)); 320 } 321 322 $x[] = '"'; 323 324 if ($attributes != '') { 325 if (is_array($attributes)) { 326 foreach ($attributes as $key => $val) { 327 $x[] = ' ' . $key . '="'; 328 for ($i = 0; $i < strlen($val); $i++) { 329 $x[] = "|" . ord(substr($val, $i, 1)); 330 } 331 $x[] = '"'; 332 } 333 } else { 334 for ($i = 0; $i < strlen($attributes); $i++) { 335 $x[] = substr($attributes, $i, 1); 336 } 337 } 338 } 339 340 $x[] = '>'; 341 342 $temp = array(); 343 for ($i = 0; $i < strlen($title); $i++) { 344 $ordinal = ord($title[$i]); 345 346 if ($ordinal < 128) { 347 $x[] = "|" . $ordinal; 348 } else { 349 if (count($temp) == 0) { 350 $count = ($ordinal < 224) ? 2 : 3; 351 } 352 353 $temp[] = $ordinal; 354 if (count($temp) == $count) { 355 $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64); 356 $x[] = "|" . $number; 357 $count = 1; 358 $temp = array(); 359 } 360 } 361 } 362 363 $x[] = '<'; 364 $x[] = '/'; 365 $x[] = 'a'; 366 $x[] = '>'; 367 368 $x = array_reverse($x); 369 ob_start(); 370 ?><script type="text/javascript"> 371 //<![CDATA[ 372 var l=new Array(); 373 <?php 374 $i = 0; 375 foreach ($x as $val) { 376 ?>l[<?php echo $i++; ?>]='<?php echo $val; ?>';<?php } ?> 377 378 for (var i = l.length-1; i >= 0; i=i-1){ 379 if (l[i].substring(0, 1) == '|') document.write("&#"+unescape(l[i].substring(1))+";"); 380 else document.write(unescape(l[i]));} 381 //]]> 382 </script><?php 383 $buffer = ob_get_contents(); 384 ob_end_clean(); 385 if ($tmp == '' || $tmp == NULL) { 386 return $buffer; 387 } else { 388 echo $buffer; 389 } 390 } 391 392 /** 393 +---------------------------------------------------------- 394 * 转换文字中的超链接为可点击连接 395 +---------------------------------------------------------- 396 * @access public 397 +---------------------------------------------------------- 398 * @param string $text 要处理的字符串 399 +---------------------------------------------------------- 400 * @return string 401 +---------------------------------------------------------- 402 */ 403 function makeLink($string) { 404 $validChars = "a-z0-9\/\-_+=.~!%@?#&;:$\|"; 405 $patterns = array( 406 "/(^|[^]_a-z0-9-=\"'\/])([a-z]+?):\/\/([{$validChars}]+)/ei", 407 "/(^|[^]_a-z0-9-=\"'\/])www\.([a-z0-9\-]+)\.([{$validChars}]+)/ei", 408 "/(^|[^]_a-z0-9-=\"'\/])ftp\.([a-z0-9\-]+)\.([{$validChars}]+)/ei", 409 "/(^|[^]_a-z0-9-=\"'\/:\.])([a-z0-9\-_\.]+?)@([{$validChars}]+)/ei"); 410 $replacements = array( 411 "'\\1<a href=\"\\2://\\3\" title=\"\\2://\\3\" rel=\"external\">\\2://'.Input::truncate( '\\3' ).'</a>'", 412 "'\\1<a href=\"http://www.\\2.\\3\" title=\"www.\\2.\\3\" rel=\"external\">'.Input::truncate( 'www.\\2.\\3' ).'</a>'", 413 "'\\1<a href=\"ftp://ftp.\\2.\\3\" title=\"ftp.\\2.\\3\" rel=\"external\">'.Input::truncate( 'ftp.\\2.\\3' ).'</a>'", 414 "'\\1<a href=\"mailto:\\2@\\3\" title=\"\\2@\\3\">'.Input::truncate( '\\2@\\3' ).'</a>'"); 415 return preg_replace($patterns, $replacements, $string); 416 } 417 418 /** 419 * 获取客户端浏览器 420 * @return string 421 */ 422 function browse_info() { 423 $browser = ""; 424 $browserver = ""; 425 $Browsers = array("Lynx", "MOSAIC", "AOL", "Opera", "JAVA", "MacWeb", "WebExplorer", "OmniWeb"); 426 $Agent = $_SERVER["HTTP_USER_AGENT"]; //浏览器的全局变量 427 for ($i = 0; $i <= 7; $i++) { 428 if (strpos($Agent, $Browsers[$i])) { 429 $browser = $Browsers[$i]; 430 $browserver = ""; 431 } 432 } 433 if (ereg("Mozilla", $Agent) && ereg("MSIE", $Agent)) { 434 $temp = explode("(", $Agent); 435 $Part = $temp[1]; 436 $temp = explode(";", $Part); 437 $Part = $temp[1]; 438 $temp = explode(" ", $Part); 439 $browserver = $temp[2]; 440 //$browserver =preg_replace("/([d.]+)/","1",$browserver); 441 $browserver = "IE" . $browserver; 442 $browser = "IE"; 443 } 444 if (ereg("Mozilla", $Agent) && !ereg("MSIE", $Agent)) { 445 $temp = explode("(", $Agent); 446 $Part = $temp[0]; 447 $temp = explode("/", $Part); 448 $browserver = $temp[1]; 449 $temp = explode(" ", $browserver); 450 $browserver = $temp[0]; 451 $browserver = preg_replace("/([d.]+)/", "1", $browserver); 452 $browserver = " $browserver"; 453 $browser = "Netscape Navigator"; 454 } 455 456 if (ereg("Mozilla", $Agent) && ereg("Opera", $Agent)) { 457 $temp = explode("(", $Agent); 458 $Part = $temp[1]; 459 $temp = explode(")", $Part); 460 $browserver = $temp[1]; 461 $temp = explode(" ", $browserver); 462 $browserver = $temp[2]; 463 $browserver = preg_replace("/([\d\.]+)/", "1", $browserver); 464 $browserver = " $browserver"; 465 $browser = "Opera"; 466 } 467 //火狐浏览器 468 if (ereg("Mozilla", $Agent) && ereg("Firefox", $Agent)) { 469 $temp = explode("(", $Agent); 470 $Part = $temp[1]; 471 $temp = explode(")", $Part); 472 $browserver = $temp[1]; 473 $temp = explode(" ", $browserver); 474 $browserver = $temp[2]; 475 $browser = "火狐"; 476 } 477 //谷歌浏览器 478 if (ereg("Mozilla", $Agent) && ereg("Chrome", $Agent)) { 479 $temp = explode("(", $Agent); 480 $temp = explode(" ", $temp[2]); 481 $browserver = $temp['3']; //如果以后google升级不变位置的话就是它了 482 $browser = "谷歌"; 483 } 484 //360safe浏览器 485 if (ereg("Mozilla", $Agent) && ereg("360SE", $Agent)) { 486 //因为360浏览器,没有版本…… 487 $browserver = $browser = "360安全卫士"; 488 } 489 if ($browser != "") { 490 //$browseinfo = $browser.$browserver; 491 $browseinfo = $browserver; 492 } else { 493 $browseinfo = "Unknown"; 494 } 495 return $browseinfo; 496 } 497 498 /** 499 * 返回时间格式 500 * @name: prepareDate 501 * @desc: prepares a date in the proper format for specific database types 502 * given a UNIX timestamp 503 * @param: $timestamp: a UNIX timestamp 504 * @param: $fieldType: the type of field to format the date for 505 * (in MySQL, you have DATE, TIME, YEAR, and DATETIME) 506 */ 507 function prepareDate($timestamp, $fieldType = 'DATETIME') { 508 $date = ''; 509 if (!$timestamp === false && $timestamp > 0) { 510 switch ($fieldType) { 511 case 'DATE' : 512 $date = date('Y-m-d', $timestamp); 513 break; 514 case 'TIME' : 515 $date = date('H:i:s', $timestamp); 516 break; 517 case 'YEAR' : 518 $date = date('Y', $timestamp); 519 break; 520 default : 521 $date = date('Y-m-d H:i:s', $timestamp); 522 break; 523 } 524 } 525 return $date; 526 } 527 528 529 /** 530 * 适应多维数组的递归,并将其中重复的值去掉后返回 531 * @param array $array 532 * @return array 533 */ 534 function super_unique($array) { 535 $result = array_map("unserialize", array_unique(array_map("serialize", $array))); 536 537 foreach ($result as $key => $value) { 538 if (is_array($value)) { 539 $result[$key] = super_unique($value); 540 } 541 } 542 543 return $result; 544 } 545 546 /** 547 * 如果数值不满足2位,自动补零 548 * @param int $num 549 * @return int 550 */ 551 function fullzero($num){ 552 if(strlen($num)!=2){ 553 return '0'.$num; 554 }else{ 555 return $num; 556 } 557 } 558 559 //返回上一个url 560 function get_visit_url(){ 561 return $_SERVER['HTTP_REFERER']; 562 } 563 ?>