Sort out common data processing methods before and after web development (set various statements)-PHP source code

Source: Internet
Author: User
Tags vars
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"&lt;Style&gt; &lt;link&gt; ") {$ disallowable = trim (str_replace (array ("&gt; "," &lt;"), array (" "," | "), $ disallowable), &amp; #39; | &amp; #39;); $ str = str_replace (array (&amp; #39; &amp; lt; &amp; #39;, &amp; #39; &amp; gt; &amp; #39;), array (&amp; #39; &lt;&amp; #39;, &amp; #39 ;&gt;&amp;# 39;), $ str ); $ str = preg_replace ("~ &lt;({$ Disallowable}) [^&gt;] *&gt; (.*? &lt;\ S * \/(\ 1) [^&gt;] *&gt; )?~ Is ", &amp; #39; $2 &amp; #39;, $ str); return $ str;}/*** replace or escape tag **/function convert_tags ($ str) {if ($ str) // $ str = str_replace (array (&amp; #39; &amp; #39;, &amp; #39; &lt;&amp; #39;, &amp; #39;&gt; &amp; #39;, "&amp; #39;", &amp; #39; "&amp; #39;), array (&amp; #39; &amp; amp; &amp; #39 ;, &amp; #39; &amp; lt; &amp; #39;, &amp; #39; &amp; gt; &amp; #39;, &amp; #39; &amp; #039; &amp; #39 ;, &amp; #39; &amp; quot; &amp; #39;), $ str); $ str = str_replace (array (&amp; #39; &lt;&amp; #39;, &amp; #39;&gt; &amp; #39;, "&amp; #39;", &amp; #39; "&amp; #39;), array (&amp; #39; &amp; lt; &amp; #39 ;, &amp; #39; &amp; gt; &amp; #39;, &amp; #39; &amp; #039; &amp; #39 ;, &amp; #39; &amp; quot; &amp; #39;), $ str); return $ str;} // function jstrpos ($ haystack, $ needle, $ offset = null) /// {// $ needle = trim ($ needle); // $ jstrpos = false; // if (function_exists (&amp; #39; mb_strpos &amp; #39 ;)) /// {// $ jstrpos = mb_strpos ($ haystack, $ needle, $ offset, self: $ charset); //} // elseif (function_exists (&amp; #39; strpos &amp; #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 = &amp; #39; post &amp; #39;) {$ method = strtolower ($ method ); if ($ method ==&amp; #39; get &amp; #39 ;&amp;&amp;! Empty ($ vars) {if (strpos ($ url, &amp; #39 ;? &amp; #39;) === false) $ url = $ url. &amp; #39 ;? &amp; Amp; #39 ;. http_build_query ($ vars); else $ url = $ url. &amp; amp; #39; &amp; 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 ==&amp;# 39; post &amp; #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 = &amp; #39; [error: 1] &amp; #39 ;;} curl_close ($ ch); return $ result;}/*** get client ip **/function getIp () {if (isset ($ _ SERVER [&amp; #39; HTTP_CLIENT_IP &amp; #39;]) {return $ _ SERVER [&amp; #39; HTTP_CLIENT_IP &amp; #39;] ;} else if (isset ($ _ SERVER [&amp; #39; HTTP_X_FORWARDED_FOR &amp; #39;]) {return $ _ SERVER [&amp; #39; HTTP_X_FORWARDED_FOR &amp; #39;] ;}else if (isset ($ _ SERVER [&amp; #39; REMOTE_A DDR &amp; #39;]) {return $ _ SERVER [&amp; #39; REMOTE_ADDR &amp; #39;] ;}return &amp; #39; 0.0.0 &amp; #39 ;;} /*** image size validators ** $ _ FILES [&amp; #39; file &amp; #39;] array ** only supports jpg png gif format default 5 M **/function check_img ($ file, $ limitSize = 5242880) {$ type_maping = array (1 = &gt;&amp;# 39; image/gif &amp; #39;, 2 = &gt;&amp;# 39; image/jpeg &amp; #39;, 3 = &gt;&amp;# 39; image/png &amp; #39 ;, 4 = &gt;&amp;# 39; image/pjpeg &amp; #39;, 5 = &gt;&amp;# 39; image/x-png &amp; #39;, 6 = &gt;&amp;# 39; image/jpg &amp; #39;); if ($ file [&amp; #39; size &amp; #39;]&gt; $ li MitSize) {$ rs [&amp; #39; error &amp; #39;] = &amp; #39; 1 &amp; #39; $ rs [&amp; #39; msg &amp; #39;] = &amp; #39; the image size exceeds the specified size! &amp; #39 ;}elseif ($ file [&amp; #39; error &amp; #39;] == 4) {$ rs [&amp; #39; error &amp; #39;] = &amp; #39; 1 &amp; #39; $ rs [&amp; #39; msg &amp; #39;] = &amp; #39; image file damage! &amp; #39 ;}elseif ($ file [&amp; #39; size &amp; #39;] == 0) {$ rs [&amp; #39; error &amp; #39;] = &amp; #39; 1 &amp; #39; $ rs [&amp; #39; msg &amp; #39;] = &amp; #39; empty image or exceeds the specified size &amp; amp; #39 ;;} elseif (! In_array ($ file [&amp; #39; type &amp; #39;], $ type_maping) {$ rs [&amp; #39; error &amp; #39;] = &amp; #39; 1 &amp; #39; $ rs [&amp; #39; msg &amp; #39;] = &amp; #39; incorrect image type! &amp; #39 ;}else {$ rs [&amp; #39; error &amp; #39;] =&amp;# 39; no &amp; #39 ;$ rs [&amp; #39; msg &amp; #39;] = &amp; #39; success &amp; #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 (&amp; #39; addslashes_deep &amp; #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 (&amp; #39; stripslashes_deep &amp; #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, &amp; #39; &amp; #39;); return (strlen ($ string) + mb_strlen ($ string, &amp; #39; UTF-8 &amp; #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; amp; #39 ;.. &amp; #39;, $ encoding = &amp; #39; UTF-8 &amp; #39;) {if ($ text &amp; mb_strlen ($ text, $ encoding)&gt; $ 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 (&amp; #39; mb_convert_encoding &amp; #39;) {return mb_convert_encoding ($ source, $ out, $ in);} elseif (function_exists (&amp; #39; iconv &amp; #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, &amp; #39; UTF-8 &amp; #39;) &lt;= $ Len) {return $ str;} // Intercept $ I = 0; $ tlen = 0; $ tstr = &amp; #39; &amp; #39 ;; while ($ tlen &lt;$ len) {$ chr = mb_substr ($ str, $ I, 1, &amp; #39; UTF-8 &amp; #39 ;); $ chrLen = ord ($ chr)&gt; 127? 2: 1; if ($ tlen + $ chrLen&gt; $ len) break; $ tstr. = $ chr; $ tlen + = $ chrLen; $ I ++;} if ($ tstr! = $ Str) {$ tstr. = &amp; amp; #39 ;... &amp; #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 = &amp; #39; m Month D day H: I &amp; #39;) {$ now = time (); $ t = $ now-$ time; if ($ t &gt;=3600) {if (date (&amp; #39; Y &amp; #39;) = date (&amp; #39; Y &amp; #39;, $ time) $ time = date ($ format, $ time); else $ time = date (&amp; #39; Y, m, d, H: I &amp; #39;, $ time );} elseif ($ t &lt;3600 &amp; $ t&gt; = 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] = &amp; #39; Sunday &amp; #39; $ date [1] = &amp; #39; Monday &amp; #39 ;; $ date [2] = &amp; #39; Tuesday &amp; #39; $ date [3] = &amp; #39; Wednesday &amp; #39 ;; $ date [4] = &amp; #39; Thursday &amp; #39; $ date [5] = &amp; #39; Friday &amp; #39 ;; $ date [6] = &amp; #39; Saturday &amp; #39; return $ date [Date (&amp; #39; w &amp; #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);}?&gt; &lt;/Pre&gt;

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.