前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >php前后台设计常用函数类

php前后台设计常用函数类

原创
作者头像
V站CEO-西顾
发布2018-06-10 11:16:51
1.4K0
发布2018-06-10 11:16:51
举报
文章被收录于专栏:V站

V站笔记

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

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 MySQL
腾讯云数据库 MySQL(TencentDB for MySQL)为用户提供安全可靠,性能卓越、易于维护的企业级云数据库服务。其具备6大企业级特性,包括企业级定制内核、企业级高可用、企业级高可靠、企业级安全、企业级扩展以及企业级智能运维。通过使用腾讯云数据库 MySQL,可实现分钟级别的数据库部署、弹性扩展以及全自动化的运维管理,不仅经济实惠,而且稳定可靠,易于运维。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档