I use the common function library

Source: Internet
Author: User
Tags ereg
There are 0 fill, intercept Chinese, halt, html jump, JS jump, filter HTML, check the legality of the mailbox, convert the text in the hyperlink as clickable connection, return time format and so on commonly used PHP function library.

  1. Because both front and rear stations need to be used, so put in the Supermario folder for easy reuse
  2. /**
  3. * Oh ~ the lovely drop of the decimal ~ Oh ~ the lovely thousand ~
  4. * If the number of digits exceeds 4 digits, the 3rd digit will be increased before
  5. * @param int $gold
  6. * @return String
  7. */
  8. function Showgold ($gold) {
  9. Return Number_format ($gold);
  10. }
  11. /**
  12. * Page Compression output
  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. * Skip the page without prompting
  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. * Clear the cache, ECHO, then die (hateful javascript+php ...) Plain text output, there will always be some empty characters ... )
  39. * @param string $msg
  40. */
  41. function Justsay ($msg) {
  42. Ob_clean ();
  43. Die ($msg);
  44. }
  45. /**
  46. * Replace the contents of the first parameter in the second parameter with the red one
  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. * Create a completely random color
  63. * @return String
  64. */
  65. function Makecolor () {
  66. $key = ' # ';
  67. for ($i = 0; $i < 6; $i + +)
  68. $key. = rand (0, 9);//Generate PHP random number
  69. return $key;
  70. }
  71. /**
  72. * Automatic loading of models and third-party function classes
  73. * @param string $className
  74. */
  75. function AutoLoad ($className) {
  76. if (strstr ($_server[' Request_uri '), "/operator/") = = False) {//server does not support 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. * Pop up a message with JavaScript
  98. * @param $message
  99. */
  100. Function message ($message = ") {
  101. Echo (');
  102. }
  103. /**
  104. * Simulates the third parameter of STRSTR (), returns the data before the $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. * Display error message and jump to $pageurl, including style
  115. *
  116. * @param string $messages
  117. * @param string $pageurl
  118. * @param int $msc number of seconds
  119. */
  120. function msg ($messages, $pageurl = ' javascript:history.back (); ', $msc =5) {
  121. Echo 'System Message
  122. Web System Message

  123. ', $messages, '
  124. >>> Please click here to return

  125. ';
  126. Exit ();
  127. }
  128. /**
  129. * Check if the later date is greater than the previous date
  130. * @param type $datecome the previous date
  131. * @param the date after type $datego
  132. * @return Type
  133. */
  134. function Sub_date ($datecome, $datego) {
  135. $d 1 = strtotime ($datecome);
  136. $d 2 = strtotime ($datego);
  137. $days = Round (($d 2-$d 1)/3600/24);
  138. $days = $days < 1? 0: $days;
  139. return $days;
  140. }
  141. /**
  142. * Replace the contents of the $length behind $string with $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 (' & ', ' "', ' < ', ' > '), $string);
  155. $strcut = ";
  156. $n = $tn = $noc = 0;
  157. while ($n < strlen ($string)) {
  158. $t = Ord ($string [$n]);
  159. if ($t = = 9 | | $t = = 10 | | (<= $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 (<= $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 (' & ', ' "', ' < ', ' > '), $strcut);
  195. Return $strcut. $dot;
  196. }
  197. /**
  198. +----------------------------------------------------------
  199. * If MAGIC_QUOTES_GPC is turned on, use this method to increment the transfer symbol before using it as a special symbol
  200. +----------------------------------------------------------
  201. * @access Public
  202. +----------------------------------------------------------
  203. * @param string $value can be an array
  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. * Remove the transfer symbol before the special symbol
  214. +----------------------------------------------------------
  215. * @access Public
  216. +----------------------------------------------------------
  217. * @param string $value can be an array
  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. * The string content HTML is manifested to avoid some illegal information directly executed. If the argument is an array, it is recursive.
  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 (' & ', ' "', ' < ', ' > '), $value));
  233. }
  234. /**
  235. * The HTML in the string is removed, if the argument is an array, then recursion.
  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. * Check the legality of 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. * Secure Email address
  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 < $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 ']%) * 4096) + (($temp [' 1 ']% *) + ($temp [' 2 ']%): (($temp [' 0 ']%) * + ($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. * Convert the hyperlinks in text to clickable links
  330. +----------------------------------------------------------
  331. * @access Public
  332. +----------------------------------------------------------
  333. * @param string $text to be processed
  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. * Get client browser
  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"]; Global variables of the browser
  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 browser
  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 Chrome
  411. if (Ereg ("Mozilla", $Agent) && ereg ("Chrome", $Agent)) {
  412. $temp = Explode ("(", $Agent);
  413. $temp = Explode ("", $temp [2]);
  414. $browserver = $temp [' 3 ']; If Google does not change the location in the future, it is.
  415. $browser = "Google";
  416. }
  417. 360safe Browser
  418. if (Ereg ("Mozilla", $Agent) && ereg ("360SE", $Agent)) {
  419. Because 360 browsers, no version ...
  420. $browserver = $browser = "360 security guard";
  421. }
  422. if ($browser! = "") {
  423. $browseinfo = $browser. $browserver;
  424. $browseinfo = $browserver;
  425. } else {
  426. $browseinfo = "Unknown";
  427. }
  428. return $browseinfo;
  429. }
  430. /**
  431. * Return time format
  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 had 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. * Adapts to the recursion of multidimensional arrays, and removes the duplicate values and returns
  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. * If the value does not meet 2 bits, auto-fill 0
  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. Return to previous URL
  486. function Get_visit_url () {
  487. Return $_server[' Http_referer '];
  488. }
  489. ?>
Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.