Organize common data processing methods before and after web development (collect various statements)
1. [code] [PHP] code
------------------------- Front-end/*** front-end public function method arrangement * @ author Weige * remarks: works with jqury. js uses * 2012-04 * // to obtain the string length function getWordSize (str) {if (! Str) return null; var length = 0; var str = str; var regDoub =/[^ x00-xff]/g; var regSingl =/[x00-xff]/g var douL = str. match (regDoub); var singL = str. match (regSingl) if (douL) {length + = douL. length * 2;} if (singL) {length + = singL. length;} length/= 2; length = Math. ceil (length); return length;} // removes spaces before and after the function trim (str) {return str. replace (/(^ \ s *) | (\ s * $)/g, '');} // function isChinese (str) {var Str = str. replace (/(^ \ s *) | (\ s * $)/g, ''); if (! (/^ [\ U4E00-\ uFA29] * $/. test (str )&&(! /^ [\ UE7C7-\ uE7F3] * $ /. test (str) {return false;} return true;} // Judge function isMobile (str) on the mobile phone) {if (/^ 1 [345689] \ d {9 }/. test (str) {return true;} return false;} // oschina // special character conversion function htmlEncode (str) {var s = ""; if (str. length = 0) return ""; // s = s. replace (// g, ""); s = str. replace (/&/g, "&"); s = s. replace (//G, ">"); s = s. replace (/\ '/g, "'"); s = s. replace (/\ "/g," "); s = s. replace (/\ n/g ,"
"); Return s;} function htmlDecode (str) {var s =" "; if (str. length = 0) return ""; s = str. replace (/>/g, "&"); s = s. replace (/</g, "<"); s = s. replace (/>/g, ">"); s = s. replace (// g, ""); s = s. replace (/'/g, "\'"); s = s. replace (/"/g," \ ""); s = s. replace (/
/G, "\ n"); return s;} // use ajax to submit the data function ajaxPost (the_url, the_param, succ_callback) {$. ajax ({type: 'post', cache: false, url: the_url, data: the_param, success: succ_callback});} // use ajax to obtain the data function ajaxGet (the_url, succ_callback, error_callback) {$. ajax ({type: 'GET', cache: true, url: the_url, success: succ_callback, error: error_callback});} // send the function ajaxPostJson (the_url, data_pro, succ_callback, error_callback) {$. ajax ({async: false, // synchronously update type: 'post', dataType: 'json', data: data_pro, url: the_url, success: succ_callback, error: error_callback});} function real_len (name) {return (name. replace (/[^ \ x00-\ xff]/g ,"**"). length);} function is_email (email) {var reg =/^ \ s * ([A-Za-z0-9 _-] + (\. \ w +) * @ (\ w + \.) + \ w {2, 3}) \ s * $/; return reg. test (email);} function is_number (name) {var reg =/^ \ d + $/g; return reg. test (name);} function is_pwd (name) {var reg =/^ [A-Za-z @ 0-9 _-] + $/g; return reg. test (name) ;}--------------------------------- backend
= 0; $ t --) {$ a = floor ($ id/pow ($ base, $ t); $ out = $ out. substr ($ index, $ a, 1); $ id = $ id-($ a * pow ($ base, $ t);} return $ out ;} /*** short link key **/function url_key ($ url, $ key = "wei ") {$ x = sprintf ("% u ", crc32 ($ key. $ url); $ show = ''; while ($ x> 0) {$ s = $ x % 62; if ($ s> 35) $ s = chr ($ s + 61); elseif ($ s> 9 & $ s <= 35) $ s = chr ($ s + 55); $ show. = $ s; $ x = floor ($ x/62);} return $ show;}/*** tag key **/function tag_key ($ tag, $ key = "wei ") {$ tag = str_replace ("", "", $ tag); $ tag. = $ key; $ hash = md5 ($ tag); $ hash = $ hash [0] | ($ hash [1] <8) | ($ hash [2] <16) | ($ hash [3] <24) | ($ hash [4] <32) | ($ hash [5] <40) | ($ hash [6] <48) | ($ hash [7] <56 ); return $ hash % 99999999;}/*** filter illegal tags **/function strip_selected_tags ($ str, $ disallowable = "script"<Style> <link> ") {$ disallowable = trim (str_replace (array ("> "," <"), array (" "," | "), $ disallowable), & #39; | & #39;); $ str = str_replace (array (& #39; & lt; & #39;, & #39; & gt; & #39;), array (& #39; <& #39;, & #39 ;>&# 39;), $ str ); $ str = preg_replace ("~ <({$ Disallowable}) [^>] *> (.*? <\ S * \/(\ 1) [^>] *> )?~ Is ", & #39; $2 & #39;, $ str); return $ str;}/*** replace or escape tag **/function convert_tags ($ str) {if ($ str) // $ str = str_replace (array (& #39; & #39;, & #39; <& #39;, & #39;> & #39;, "& #39;", & #39; "& #39;), array (& #39; & amp; & #39 ;, & #39; & lt; & #39;, & #39; & gt; & #39;, & #39; & #039; & #39 ;, & #39; & quot; & #39;), $ str); $ str = str_replace (array (& #39; <& #39;, & #39;> & #39;, "& #39;", & #39; "& #39;), array (& #39; & lt; & #39 ;, & #39; & gt; & #39;, & #39; & #039; & #39 ;, & #39; & quot; & #39;), $ str); return $ str;} // function jstrpos ($ haystack, $ needle, $ offset = null) /// {// $ needle = trim ($ needle); // $ jstrpos = false; // if (function_exists (& #39; mb_strpos & #39 ;)) /// {// $ jstrpos = mb_strpos ($ haystack, $ needle, $ offset, self: $ charset); //} // elseif (function_exists (& #39; strpos & #39;) // {// $ jstrpos = strpos ($ haystack, $ needle, $ offset); //} // return $ jstrpos; //}/*** Check whether it starts with an http. com url **/function is_http ($ url) {if (filter_var ($ url, FILTER_VALIDATE_URL) return true; return false ;} /*** send an http request * @ param $ url request link * @ param $ method request method * @ param array $ vars request parameter * @ param $ time_out request expiration time *@ return JsonObj */function get_curl ($ url, array $ vars = array (), $ method = & #39; post & #39;) {$ method = strtolower ($ method ); if ($ method ==& #39; get & #39 ;&&! Empty ($ vars) {if (strpos ($ url, & #39 ;? & #39;) === false) $ url = $ url. & #39 ;? & Amp; #39 ;. http_build_query ($ vars); else $ url = $ url. & amp; #39; & amp; #39 ;. http_build_query ($ vars);} $ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, 5); curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); if ($ method ==&# 39; post & #39;) {curl_setopt ($ ch, CURLOPT_POST, 1); curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ vars);} $ r Esult = curl_exec ($ ch); if (! Curl_errno ($ ch) {$ result = trim ($ result);} else {$ result = & #39; [error: 1] & #39 ;;} curl_close ($ ch); return $ result;}/*** get client ip **/function getIp () {if (isset ($ _ SERVER [& #39; HTTP_CLIENT_IP & #39;]) {return $ _ SERVER [& #39; HTTP_CLIENT_IP & #39;] ;} else if (isset ($ _ SERVER [& #39; HTTP_X_FORWARDED_FOR & #39;]) {return $ _ SERVER [& #39; HTTP_X_FORWARDED_FOR & #39;] ;}else if (isset ($ _ SERVER [& #39; REMOTE_A DDR & #39;]) {return $ _ SERVER [& #39; REMOTE_ADDR & #39;] ;}return & #39; 0.0.0 & #39 ;;} /*** image size validators ** $ _ FILES [& #39; file & #39;] array ** only supports jpg png gif format default 5 M **/function check_img ($ file, $ limitSize = 5242880) {$ type_maping = array (1 = >&# 39; image/gif & #39;, 2 = >&# 39; image/jpeg & #39;, 3 = >&# 39; image/png & #39 ;, 4 = >&# 39; image/pjpeg & #39;, 5 = >&# 39; image/x-png & #39;, 6 = >&# 39; image/jpg & #39;); if ($ file [& #39; size & #39;]> $ li MitSize) {$ rs [& #39; error & #39;] = & #39; 1 & #39; $ rs [& #39; msg & #39;] = & #39; the image size exceeds the specified size! & #39 ;}elseif ($ file [& #39; error & #39;] == 4) {$ rs [& #39; error & #39;] = & #39; 1 & #39; $ rs [& #39; msg & #39;] = & #39; image file damage! & #39 ;}elseif ($ file [& #39; size & #39;] == 0) {$ rs [& #39; error & #39;] = & #39; 1 & #39; $ rs [& #39; msg & #39;] = & #39; empty image or exceeds the specified size & amp; #39 ;;} elseif (! In_array ($ file [& #39; type & #39;], $ type_maping) {$ rs [& #39; error & #39;] = & #39; 1 & #39; $ rs [& #39; msg & #39;] = & #39; incorrect image type! & #39 ;}else {$ rs [& #39; error & #39;] =&# 39; no & #39 ;$ rs [& #39; msg & #39;] = & #39; success & #39;} return $ rs ;} /*** use recursion to escape special characters in the variable and filter the tag */function addslashes_deep ($ value) {if (empty ($ value) return $ value; // Huige filters html tags to prevent SQL injection of return is_array ($ value )? Array_map (& #39; addslashes_deep & #39;, $ value): strip_tags (addslashes ($ value ));} /*** @ param mix $ value * @ return mix */function stripslashes_deep ($ value) {if (empty ($ value) return $ value; return is_array ($ value )? Array_map (& #39; stripslashes_deep & #39;, $ value): stripslashes ($ value) ;}/ *** count the number of words on Sina Weibo (simple version) * one Chinese character, 0.5 English letters, 1 full-width character, and 0.5 half-width characters. * @ Param string $ string * @ return integer */function strlen_weibo ($ string) {if (is_string ($ string) {$ string = trim ($ string, & #39; & #39;); return (strlen ($ string) + mb_strlen ($ string, & #39; UTF-8 & #39;)/4 ;} return false;}/*** intercept a string of the specified length. if the length is exceeded, use .. replace * @ param string $ text * @ param int $ length * @ param string $ replace * @ param string $ encoding */function substr_format ($ text, $ length, $ replac E = & amp; #39 ;.. & #39;, $ encoding = & #39; UTF-8 & #39;) {if ($ text & mb_strlen ($ text, $ encoding)> $ length) {return mb_substr ($ text, 0, $ length, $ encoding ). $ replace;} return $ text;}/***** character encoding conversion ***/function xwb_iconv ($ source, $ in, $ out) {$ in = strtoupper ($ in); $ out = strtoupper ($ out); if ($ in = "UTF8") {$ in = "UTF-8 ";} if ($ out = "UTF8") {$ out = "UTF-8";} if ($ in = $ out) {return $ source;} if (funct Ion_exists (& #39; mb_convert_encoding & #39;) {return mb_convert_encoding ($ source, $ out, $ in);} elseif (function_exists (& #39; iconv & #39;) {return iconv ($ in, $ out. "// IGNORE", $ source);} return $ source;}/*** Created: 2010-10-28 ** extract a certain length string * @ Author guoliang1 **************************** * **********************/function cut_string ($ str, $ len) {// check the length if (mb_strwidth ($ str, & #39; UTF-8 & #39;) <= $ Len) {return $ str;} // Intercept $ I = 0; $ tlen = 0; $ tstr = & #39; & #39 ;; while ($ tlen <$ len) {$ chr = mb_substr ($ str, $ I, 1, & #39; UTF-8 & #39 ;); $ chrLen = ord ($ chr)> 127? 2: 1; if ($ tlen + $ chrLen> $ len) break; $ tstr. = $ chr; $ tlen + = $ chrLen; $ I ++;} if ($ tstr! = $ Str) {$ tstr. = & amp; #39 ;... & #39 ;}return $ tstr ;}/ *** Created: 2010-10-28 ** prevents XSS attacks, htmlspecialchars alias ************************************* * *************/function escape ($ str, $ quote_style = ENT_COMPAT) {return htmlspecialchars ($ str, $ quote_style);}/*** format time ***/function wb_date_format ($ time, $ format = & #39; m Month D day H: I & #39;) {$ now = time (); $ t = $ now-$ time; if ($ t >=3600) {if (date (& #39; Y & #39;) = date (& #39; Y & #39;, $ time) $ time = date ($ format, $ time); else $ time = date (& #39; Y, m, d, H: I & #39;, $ time );} elseif ($ t <3600 & $ t> = 60) $ time = floor ($ t/60 ). "minutes ago"; else $ time = "just"; return $ time;} function isChinese ($ string) {if (preg_match ("/^ [\ x {4e00}-\ x {9fa5}] + $/u", $ string) return true; return false ;} function isMobile ($ mobile) {if (preg_match ("/^ 1 [345689] \ d {9} $/", $ mobile )) Return true; return false;} function dayToWeek ($ time) {$ time = empty ($ time )? TIME: $ time; $ date [0] = & #39; Sunday & #39; $ date [1] = & #39; Monday & #39 ;; $ date [2] = & #39; Tuesday & #39; $ date [3] = & #39; Wednesday & #39 ;; $ date [4] = & #39; Thursday & #39; $ date [5] = & #39; Friday & #39 ;; $ date [6] = & #39; Saturday & #39; return $ date [Date (& #39; w & #39;, $ time)];} /*** check whether the email address is * @ param $ val * @ param $ domain * @ return boolean */function is_email ($ val, $ domain = "") {if (! $ Domain) {if (preg_match ("/^ [a-z0-9-_.] + @ [\ da-z] [\. \ w-] + \. [a-z] {2, 4} $/I ", $ val) return TRUE; return FALSE;} if (preg_match ("/^ [a-z0-9-_.] + @". $ domain. "$/I", $ val) return TRUE; return FALSE;} // Mr. junz // http://www.oschina.net/code/snippet_162279_7186 // Reversible encryption function extend_decrypt ($ encryptedText, $ key) {$ cryptText = base64_decode ($ encryptedText); $ ivSize = mcrypt_get_iv_size (encrypt, MCRYPT_MODE_ECB ); $ iv = mcrypt_create_iv ($ ivSize, MCRYPT_RAND); $ decryptText = mcrypt_decrypt (encrypt, $ key, $ cryptText, encrypt, $ iv); return trim ($ decryptText );}// http://www.oschina.net/code/snippet_162279_7186 // Reversible decryption function extend_encrypt ($ plainText, $ key) {$ ivSize = mcrypt_get_iv_size (encrypt, MCRYPT_MODE_ECB); $ iv = mcrypt_create_iv ($ ivSize, MCRYPT_RAND ); $ encryptText = mcrypt_encrypt (MCRYPT_RIJNDAEL_256, $ key, $ plainText, MCRYPT_MODE_ECB, $ iv); return trim (base64_encode ($ encryptText);}?> </Pre>