/*** removes whitespace */function trim (str) {return (str + ") at the left and right ends of the string. Replace (/(\s+) $/g,"). Replace (/^\s+/g, '); /*** Gets the string length (ie: if it is ie, and a newline character (\ n) is replaced with ' _ ' and then the length is calculated) */function strlen (str) {//Gets the string length return (browser.ie && str. IndexOf (' \ n ')! =-1)? Str.replace (/\r?\n/g, ' _ '). Length:str.length;} /*** get Word character string length */function Mb_strlen (str) {var len = 0;//Declaration length for (var i = 0; i < str.length; i++) {//traverse str parameter Len + = Str.charcodeat (i) < 0 | | Str.charcodeat (i) > 255? (CharSet = = ' Utf-8 '? 3:2): 1;//charcodeat (i) >255 is used to judge unconventional characters, if it is encoded in UTF-8, a Chinese character occupies 3 bytes, a middle character placeholder is 2 bytes, the English character is 1 bytes} return Len; Returns the length of the string}/*** the text beyond the specified maximum length (maxlen) as dot (' ... ') */function mb_cutstr (str, maxlen, dot) {var len = 0; Declaration length var ret = '; Declare return value var dot =!dot? ' ... ': dot; Determine if dot is empty, if empty dot defaults to ' ... ' maxlen = maxlen-dot.length; Gets the length value between the maximum length and dot for (var i = 0; i < str.length; i++) {//traversal target character str len + str.charcodeat (i) < 0 | | str . charCodeAt (i) > 255? (CharSet = = ' Utf-8 '? 3:2): 1; In the case of a UTF-8 encoding, a Chinese character occupies 3 bytes, the placeholder for a medium character is 2 bytes, the English character is 1 bytes if (len > MaxLen) {//determines if Len is greater than the maximum length ret + + dot; If it is greater than the maximum length, add dot to jump out of the loop break; Bounce Loop} ret + = Str.substr (i, 1); Get one word at a time and stitch behind the RET} return ret; Returns the result}/*** the value matched to search in Str is replaced with replace* if search, replace is an array, it can be understood as a multiple substitution * if search:[' 1 ', ' 2 ', ' replace:[' a ', ' B '], Then replace 1 with a, replace 2 with B, and if search is longer than replace, the default is replaced with the first value of Replace * search: regular; Replace: The value that needs to be replaced (can be an array); STR: find source; Regswitch: Regular modifier (desirable value: ' I ', ' g ', ' IG ', etc.) */function preg_replace (search, replace, str, regswitch) {var regswitch =!REGSWITC H? ' IG ': regswitch; Set modifier, default value if Regswitch is empty: ' IG ' var len = search.length; Get search length for (var i = 0; i < len; i++) {//traverse len re = new RegExp (Search[i], regswitch); Declare the regular, and set the modifier//to replace the value matched to re in STR with replace if replace is a string, and replace if it is an array, replace with the corresponding value in the current replace str = str.replace (r E, typeof replace = = ' string '? Replace: (Replace[i]? Replace[i]: replace[0]); } return str;//returns the result}
Discuz Common.js Code Comment (c)