当前位置:Gxlcms > PHP教程 > 我常用的commonfunction库

我常用的commonfunction库

时间:2021-07-01 10:21:17 帮助过:4人阅读

有零填充、截取中文、halt、html跳转、js跳转、过滤html、检查邮箱合法、转换文字中的超链接为可点击连接、返回时间格式等等常用php函数库。

  1. //因为前后台都要用到,所以放在supermario文件夹下方便重用
  2. /**
  3. * 喔~可爱滴小数点~喔~可爱滴千位数~
  4. * 如果位数超过4位,则将第3位前增加,
  5. * @param int $gold
  6. * @return string
  7. */
  8. function showGold($gold) {
  9. return number_format($gold);
  10. }
  11. /**
  12. * 页面压缩输出
  13. */
  14. function ob_callback($buffer) {
  15. header('Etag: '.md5($buffer));
  16. if( extension_loaded('zlib') AND strstr($_SERVER["HTTP_ACCEPT_ENCODING"],"gzip") ) {
  17. $buffer = gzencode($buffer,9);
  18. header('Content-Encoding: gzip');
  19. header('Vary: Accept-Encoding');
  20. //header('Via: www.guangxitravel.cn');
  21. header('Content-Length: '.strlen($buffer));
  22. }
  23. return $buffer;
  24. }
  25. /**
  26. * 没有提示的情况下跳转页面
  27. * @param string $url
  28. * @param string $target
  29. */
  30. function go($url='', $target='parent') {
  31. if ($url != "") {
  32. echo('');
  33. } else {
  34. echo('');
  35. }
  36. }
  37. /**
  38. * 清除缓存,ECHO,然后DIE(可恶的javascript+php……纯文本输出,总会有一些空字符……)
  39. * @param string $msg
  40. */
  41. function justSay($msg) {
  42. ob_clean();
  43. die($msg);
  44. }
  45. /**
  46. * 将符合第二个参数的第一个参数里的内容替换为红色
  47. * @param string $data
  48. * @param array $keyword
  49. * @return string
  50. */
  51. function change_keyword($data, $keywords) {
  52. if (is_array($keywords)) {
  53. foreach ($keywords as $k => $v) {
  54. $data = str_replace($v, '' . $v . '', $data);
  55. }
  56. return $data;
  57. } else {
  58. return str_replace($keywords, '' . $keywords . '', $data);
  59. }
  60. }
  61. /**
  62. * 创建完全随机的颜色
  63. * @return string
  64. */
  65. function makeColor() {
  66. $key = '#';
  67. for ($i = 0; $i < 6; $i++)
  68. $key.= rand(0, 9);//生成php随机数
  69. return $key;
  70. }
  71. /**
  72. * 自动加载模型和第三方功能类
  73. * @param string $className
  74. */
  75. function autoLoad($className) {
  76. if (strstr($_SERVER['REQUEST_URI'], "/operator/") == false) { //服务器不支持SCRIPT_URL
  77. if (file_exists(siteRoot . '/client/model/' . $className . '.php')) {
  78. require_once siteRoot . '/client/model/' . $className . '.php';
  79. return;
  80. }
  81. } else {
  82. if (file_exists(siteRoot . '/manager/model/' . $className . '.php')) {
  83. require_once siteRoot . '/manager/model/' . $className . '.php';
  84. return;
  85. }
  86. }
  87. if (file_exists(siteRoot . '/public/modules/' . $className . '.php')) {
  88. require_once siteRoot . '/public/modules/' . $className . '.php';
  89. return;
  90. }
  91. if (file_exists(siteRoot . '/client/order/model/' . $className . '.php')) {
  92. require_once siteRoot . '/client/order/model/' . $className . '.php';
  93. return;
  94. }
  95. }
  96. /**
  97. * 用javascript弹出一条信息
  98. * @param $message
  99. */
  100. function message($message='') {
  101. echo('');
  102. }
  103. /**
  104. * 模拟strstr()的第三个参数,返回$h中,$n之前的数据
  105. * //$h = haystack, $n = needle
  106. * @param $h
  107. * @param $n
  108. * @return
  109. */
  110. function strstrb($h, $n) {
  111. return array_shift(explode($n, $h, 2));
  112. }
  113. /**
  114. * 显示错误信息,并跳转至$pageurl,含有样式
  115. *
  116. * @param string $messages
  117. * @param string $pageurl
  118. * @param int $msc 秒数
  119. */
  120. function msg($messages, $pageurl='javascript:history.back();', $msc=5) {
  121. echo 'System Message
  122. Web System Message

  123. ', $messages, '
  124. >>>请点此处返回

  125. ';
  126. exit();
  127. }
  128. /**
  129. * 检查后面的日期是否大于前面的日期
  130. * @param type $datecome 前面的日期
  131. * @param type $datego 后面的日期
  132. * @return type
  133. */
  134. function sub_date($datecome, $datego) {
  135. $d1 = strtotime($datecome);
  136. $d2 = strtotime($datego);
  137. $days = round(($d2 - $d1) / 3600 / 24);
  138. $days = $days < 1 ? 0 : $days;
  139. return $days;
  140. }
  141. /**
  142. * 将$string的$length后边的内容用$dot替换
  143. * @param string $string
  144. * @param int $length
  145. * @param string $dot
  146. * @return string
  147. */
  148. function cutstr($string, $length, $dot = '...') {
  149. @extract($string);
  150. if (strlen($string) <= $length) {
  151. return $string;
  152. }
  153. $string = htmlspecialchars($string);
  154. $string = str_replace(array('&', '"', '<', '>'), array('&', '"', '<', '>'), $string);
  155. $strcut = '';
  156. $n = $tn = $noc = 0;
  157. while ($n < strlen($string)) {
  158. $t = ord($string[$n]);
  159. if ($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
  160. $tn = 1;
  161. $n++;
  162. $noc++;
  163. } elseif (194 <= $t && $t <= 223) {
  164. $tn = 2;
  165. $n += 2;
  166. $noc += 2;
  167. } elseif (224 <= $t && $t < 239) {
  168. $tn = 3;
  169. $n += 3;
  170. $noc += 2;
  171. } elseif (240 <= $t && $t <= 247) {
  172. $tn = 4;
  173. $n += 4;
  174. $noc += 2;
  175. } elseif (248 <= $t && $t <= 251) {
  176. $tn = 5;
  177. $n += 5;
  178. $noc += 2;
  179. } elseif ($t == 252 || $t == 253) {
  180. $tn = 6;
  181. $n += 6;
  182. $noc += 2;
  183. } else {
  184. $n++;
  185. }
  186. if ($noc >= $length) {
  187. break;
  188. }
  189. }
  190. if ($noc > $length) {
  191. $n -= $tn;
  192. }
  193. $strcut = substr($string, 0, $n);
  194. $strcut = str_replace(array('&', '"', '<', '>'), array('&', '"', '<', '>'), $strcut);
  195. return $strcut . $dot;
  196. }
  197. /**
  198. +----------------------------------------------------------
  199. * 如果 magic_quotes_gpc 为开启状态,则使用此方法使用为特殊符号前增加转移符号
  200. +----------------------------------------------------------
  201. * @access public
  202. +----------------------------------------------------------
  203. * @param string $value 可以为数组
  204. +----------------------------------------------------------
  205. * @return string
  206. +----------------------------------------------------------
  207. */
  208. function kaddslashes($value) {
  209. return $value = is_array($value) ? array_map('kaddslashes', $value) : addslashes($value);
  210. }
  211. /**
  212. +----------------------------------------------------------
  213. * 为特殊符号前去除转移符号
  214. +----------------------------------------------------------
  215. * @access public
  216. +----------------------------------------------------------
  217. * @param string $value 可以为数组
  218. +----------------------------------------------------------
  219. * @return string
  220. +----------------------------------------------------------
  221. */
  222. function kstripcslashes($value) {
  223. return $value = is_array($value) ? array_map('kstripcslashes', $value) : stripcslashes($value);
  224. }
  225. /**
  226. * 将字符串内容html实体化,避免一些非法信息直接执行。如果参数是数组,则递归。
  227. * @param string $value
  228. * @return array
  229. */
  230. function khtmlspecialchars($value) {
  231. return is_array($value) ? array_map('khtmlspecialchars', $value) :
  232. 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));
  233. }
  234. /**
  235. * 将字符串中的html去除,如果参数是数组,则递归。
  236. * @param string $value
  237. * @return array
  238. */
  239. function striptags($value) {
  240. return $value = is_array($value) ? array_map('striptags', $value) : strip_tags($value);
  241. }
  242. /**
  243. * 检查email的合法性
  244. *
  245. * @param string $email
  246. * @return bool
  247. */
  248. function check_email($email) {
  249. if (preg_match("/([a-z0-9]*[-_\.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z]{2,3}([\.][a-z]{2})?/i", $email)) {
  250. return true;
  251. } else {
  252. return false;
  253. }
  254. }
  255. /**
  256. * 安全电子邮件地址
  257. *
  258. * @param string $email
  259. * @param string $title
  260. * @param $attributes
  261. * @return
  262. */
  263. function safe_mailto($email, $title = '', $attributes = '') {
  264. if (is_array($email)) {
  265. $tmp = $email;
  266. unset($email);
  267. extract($tmp);
  268. }
  269. $title = (string) $title;
  270. if ($title == "") {
  271. $title = $email;
  272. }
  273. for ($i = 0; $i < 16; $i++) {
  274. $x[] = substr(' }
  275. for ($i = 0; $i < strlen($email); $i++) {
  276. $x[] = "|" . ord(substr($email, $i, 1));
  277. }
  278. $x[] = '"';
  279. if ($attributes != '') {
  280. if (is_array($attributes)) {
  281. foreach ($attributes as $key => $val) {
  282. $x[] = ' ' . $key . '="';
  283. for ($i = 0; $i < strlen($val); $i++) {
  284. $x[] = "|" . ord(substr($val, $i, 1));
  285. }
  286. $x[] = '"';
  287. }
  288. } else {
  289. for ($i = 0; $i < strlen($attributes); $i++) {
  290. $x[] = substr($attributes, $i, 1);
  291. }
  292. }
  293. }
  294. $x[] = '>';
  295. $temp = array();
  296. for ($i = 0; $i < strlen($title); $i++) {
  297. $ordinal = ord($title[$i]);
  298. if ($ordinal < 128) {
  299. $x[] = "|" . $ordinal;
  300. } else {
  301. if (count($temp) == 0) {
  302. $count = ($ordinal < 224) ? 2 : 3;
  303. }
  304. $temp[] = $ordinal;
  305. if (count($temp) == $count) {
  306. $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64);
  307. $x[] = "|" . $number;
  308. $count = 1;
  309. $temp = array();
  310. }
  311. }
  312. }
  313. $x[] = '<';
  314. $x[] = '/';
  315. $x[] = 'a';
  316. $x[] = '>';
  317. $x = array_reverse($x);
  318. ob_start();
  319. ?> $buffer = ob_get_contents();
  320. ob_end_clean();
  321. if ($tmp == '' || $tmp == NULL) {
  322. return $buffer;
  323. } else {
  324. echo $buffer;
  325. }
  326. }
  327. /**
  328. +----------------------------------------------------------
  329. * 转换文字中的超链接为可点击连接
  330. +----------------------------------------------------------
  331. * @access public
  332. +----------------------------------------------------------
  333. * @param string $text 要处理的字符串
  334. +----------------------------------------------------------
  335. * @return string
  336. +----------------------------------------------------------
  337. */
  338. function makeLink($string) {
  339. $validChars = "a-z0-9\/\-_+=.~!%@?#&;:$\|";
  340. $patterns = array(
  341. "/(^|[^]_a-z0-9-=\"'\/])([a-z]+?):\/\/([{$validChars}]+)/ei",
  342. "/(^|[^]_a-z0-9-=\"'\/])www\.([a-z0-9\-]+)\.([{$validChars}]+)/ei",
  343. "/(^|[^]_a-z0-9-=\"'\/])ftp\.([a-z0-9\-]+)\.([{$validChars}]+)/ei",
  344. "/(^|[^]_a-z0-9-=\"'\/:\.])([a-z0-9\-_\.]+?)@([{$validChars}]+)/ei");
  345. $replacements = array(
  346. "'\\1\\2://'.Input::truncate( '\\3' ).''",
  347. "'\\1'.Input::truncate( 'www.\\2.\\3' ).''",
  348. "'\\1'.Input::truncate( 'ftp.\\2.\\3' ).''",
  349. "'\\1'.Input::truncate( '\\2@\\3' ).''");
  350. return preg_replace($patterns, $replacements, $string);
  351. }
  352. /**
  353. * 获取客户端浏览器
  354. * @return string
  355. */
  356. function browse_info() {
  357. $browser = "";
  358. $browserver = "";
  359. $Browsers = array("Lynx", "MOSAIC", "AOL", "Opera", "JAVA", "MacWeb", "WebExplorer", "OmniWeb");
  360. $Agent = $_SERVER["HTTP_USER_AGENT"]; //浏览器的全局变量
  361. for ($i = 0; $i <= 7; $i++) {
  362. if (strpos($Agent, $Browsers[$i])) {
  363. $browser = $Browsers[$i];
  364. $browserver = "";
  365. }
  366. }
  367. if (ereg("Mozilla", $Agent) && ereg("MSIE", $Agent)) {
  368. $temp = explode("(", $Agent);
  369. $Part = $temp[1];
  370. $temp = explode(";", $Part);
  371. $Part = $temp[1];
  372. $temp = explode(" ", $Part);
  373. $browserver = $temp[2];
  374. //$browserver =preg_replace("/([d.]+)/","1",$browserver);
  375. $browserver = "IE" . $browserver;
  376. $browser = "IE";
  377. }
  378. if (ereg("Mozilla", $Agent) && !ereg("MSIE", $Agent)) {
  379. $temp = explode("(", $Agent);
  380. $Part = $temp[0];
  381. $temp = explode("/", $Part);
  382. $browserver = $temp[1];
  383. $temp = explode(" ", $browserver);
  384. $browserver = $temp[0];
  385. $browserver = preg_replace("/([d.]+)/", "1", $browserver);
  386. $browserver = " $browserver";
  387. $browser = "Netscape Navigator";
  388. }
  389. if (ereg("Mozilla", $Agent) && ereg("Opera", $Agent)) {
  390. $temp = explode("(", $Agent);
  391. $Part = $temp[1];
  392. $temp = explode(")", $Part);
  393. $browserver = $temp[1];
  394. $temp = explode(" ", $browserver);
  395. $browserver = $temp[2];
  396. $browserver = preg_replace("/([\d\.]+)/", "1", $browserver);
  397. $browserver = " $browserver";
  398. $browser = "Opera";
  399. }
  400. //火狐浏览器
  401. if (ereg("Mozilla", $Agent) && ereg("Firefox", $Agent)) {
  402. $temp = explode("(", $Agent);
  403. $Part = $temp[1];
  404. $temp = explode(")", $Part);
  405. $browserver = $temp[1];
  406. $temp = explode(" ", $browserver);
  407. $browserver = $temp[2];
  408. $browser = "火狐";
  409. }
  410. //谷歌浏览器
  411. if (ereg("Mozilla", $Agent) && ereg("Chrome", $Agent)) {
  412. $temp = explode("(", $Agent);
  413. $temp = explode(" ", $temp[2]);
  414. $browserver = $temp['3']; //如果以后google升级不变位置的话就是它了
  415. $browser = "谷歌";
  416. }
  417. //360safe浏览器
  418. if (ereg("Mozilla", $Agent) && ereg("360SE", $Agent)) {
  419. //因为360浏览器,没有版本……
  420. $browserver = $browser = "360安全卫士";
  421. }
  422. if ($browser != "") {
  423. //$browseinfo = $browser.$browserver;
  424. $browseinfo = $browserver;
  425. } else {
  426. $browseinfo = "Unknown";
  427. }
  428. return $browseinfo;
  429. }
  430. /**
  431. * 返回时间格式
  432. * @name: prepareDate
  433. * @desc: prepares a date in the proper format for specific database types
  434. * given a UNIX timestamp
  435. * @param: $timestamp: a UNIX timestamp
  436. * @param: $fieldType: the type of field to format the date for
  437. * (in MySQL, you have DATE, TIME, YEAR, and DATETIME)
  438. */
  439. function prepareDate($timestamp, $fieldType = 'DATETIME') {
  440. $date = '';
  441. if (!$timestamp === false && $timestamp > 0) {
  442. switch ($fieldType) {
  443. case 'DATE' :
  444. $date = date('Y-m-d', $timestamp);
  445. break;
  446. case 'TIME' :
  447. $date = date('H:i:s', $timestamp);
  448. break;
  449. case 'YEAR' :
  450. $date = date('Y', $timestamp);
  451. break;
  452. default :
  453. $date = date('Y-m-d H:i:s', $timestamp);
  454. break;
  455. }
  456. }
  457. return $date;
  458. }
  459. /**
  460. * 适应多维数组的递归,并将其中重复的值去掉后返回
  461. * @param array $array
  462. * @return array
  463. */
  464. function super_unique($array) {
  465. $result = array_map("unserialize", array_unique(array_map("serialize", $array)));
  466. foreach ($result as $key => $value) {
  467. if (is_array($value)) {
  468. $result[$key] = super_unique($value);
  469. }
  470. }
  471. return $result;
  472. }
  473. /**
  474. * 如果数值不满足2位,自动补零
  475. * @param int $num
  476. * @return int
  477. */
  478. function fullzero($num){
  479. if(strlen($num)!=2){
  480. return '0'.$num;
  481. }else{
  482. return $num;
  483. }
  484. }
  485. //返回上一个url
  486. function get_visit_url(){
  487. return $_SERVER['HTTP_REFERER'];
  488. }
  489. ?>

人气教程排行