我常用的common function庫

來源:互聯網
上載者:User
有零填充、截取中文、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……純文字輸出,總會有一些Null 字元……)
  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. //Firefox瀏覽器
  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 = "Firefox";
  409. }
  410. //Google瀏覽器
  411. if (ereg("Mozilla", $Agent) && ereg("Chrome", $Agent)) {
  412. $temp = explode("(", $Agent);
  413. $temp = explode(" ", $temp[2]);
  414. $browserver = $temp['3']; //如果以後google升級不變位置的話就是它了
  415. $browser = "Google";
  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. ?>
複製代碼
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.