package com.zehfernando.utils { /** * @author Zeh Fernando - z at zeh.com.br */ public class StringUtils { public static const VALIDATION_EMAIL:RegExp = /^[a-z][\w.-]+@\w[\w.-]+\.[\w.-]*[a-z][a-z]$/i; protected static var uniqueSerialNumber:int = 0; public static function stripDoubleCRLF(__text:String): String { if (__text == null) return null; return __text.split("\r\n").join("\n"); } public static function wrapSpanStyle(__text:String, __style:String = null): String { return (Boolean(__style) ? "" : "") + __text + ""; } public static function wrapCDATA(__text:String):String { return ""; } public static function stripInvalidFileCharacters(__text:String): String { __text = __text.split(":").join(""); return __text; } public static function makeStub(__text:String): String { // Transforms a title into a stub return __text.toLowerCase().replace(" ", "-").replace(/[^a-z0-9\-]/gi, ""); } public static function parseBBCodeToHTML(__text:String): String { var rx:RegExp; // For when /gi does not work // \r\n __text = __text.replace(/\r\n/gi, "\n"); // [size="n"]...[/size] // ... rx = /\[size=\u0022([0-9]*?)\u0022\]((.|\n|\r)*?)\[\/size\]?/i; while (rx.test(__text)) __text = __text.replace(rx, "$2"); // [color="c"]...[/color] // ... rx = /\[color=\u0022(#[0-9a-f]*?)\u0022\]((.|\n|\r)*?)\[\/color\]?/i; while (rx.test(__text)) __text = __text.replace(rx, "$2"); // [url="u"]...[/url] // ... rx = /\[url=\u0022(.*?)\u0022\]((.|\n|\r)*?)\[\/url\]?/i; while (rx.test(__text)) __text = __text.replace(rx, "$2"); // [b]...[/b] // ... rx = /\[b\]((.|\n|\r)*?)\[\/b\]?/i; while (rx.test(__text)) __text = __text.replace(rx, "$1"); // [i]...[/i] // ... rx = /\[i\]((.|\n|\r)*?)\[\/i\]?/i; while (rx.test(__text)) __text = __text.replace(rx, "$1"); return (__text); } public static function cropText(__text:String, __maximumLength:Number, __breakAnywhere:Boolean = false, __postText:String = ""):String { if (__text.length <= __maximumLength) return __text; // Crops a long text, to get excerpts if (__breakAnywhere) { // Break anywhere return __text.substr(0, Math.min(__maximumLength, __text.length)) + __postText; } // Break on words only var lastIndex:Number = 0; var prevIndex:Number = -1; while (lastIndex < __maximumLength && lastIndex > -1) { prevIndex = lastIndex; lastIndex = __text.indexOf(" ", lastIndex+1); } if (prevIndex == -1) { trace ("##### COULD NOT CROP ==> ", prevIndex, lastIndex, __text); prevIndex = __maximumLength; } return __text.substr(0, Math.max(0, prevIndex)) + __postText; } public static function getQuerystringParameterValue(__url:String, __parameterName:String): String { // Finds the value of a parameter given a querystring/url and the parameter name var qsRegex:RegExp = new RegExp("[?&]" + __parameterName + "(?:=([^&]*))?","i"); var match:Object = qsRegex.exec(__url); if (Boolean(match)) return match[1]; return null; } public static function replaceHTMLLinks(__text:String, __target:String = "_blank", __twitterSearchTemplate:String = "http://twitter.com/search?q=[[string]]", __twitterUserTemplate:String = "http://twitter.com/[[user]]"): String { // Create links for urls, hashtags and whatnot on the text var regexSearch:RegExp; var regexResult:Object; var str:String; var linkStart:Vector. = new Vector.(); var linkLength:Vector. = new Vector.(); var linkURL:Vector. = new Vector.(); var i:int; // Links for hashtags //regexSearch = /\s#+\w*(\s|,|!|\.|\n)*?/gi; regexSearch = /\B#+\w*(\s|,|!|\.|\n)*?/gi; regexResult = regexSearch.exec(__text); while (regexResult != null) { str = regexResult[0]; linkURL.push(__twitterSearchTemplate.split("[[string]]").join(escape(str))); linkStart.push(regexResult["index"]); linkLength.push(str.length); regexResult = regexSearch.exec(__text); } // Links for user names regexSearch = /@+\w*(\s|,|!|\.|\n)*?/gi; regexResult = regexSearch.exec(__text); while (regexResult != null) { str = regexResult[0]; // Inserts in a sorted manner otherwise it won't work when looping for (i = 0; i <= linkStart.length; i++) { if (i == linkStart.length || regexResult["index"] < linkStart[i]) { linkURL.splice(i, 0, __twitterUserTemplate.split("[[user]]").join(str.substr(1))); linkStart.splice(i, 0, regexResult["index"]); linkLength.splice(i, 0, str.length); break; } } regexResult = regexSearch.exec(__text); } // Links for URLs regexSearch = /(http:\/\/+[\S]*)/gi; regexResult = regexSearch.exec(__text); while (regexResult != null) { str = regexResult[0]; // Inserts in a sorted manner otherwise it won't work when looping for (i = 0; i <= linkStart.length; i++) { if (i == linkStart.length || regexResult["index"] < linkStart[i]) { linkURL.splice(i, 0, str); linkStart.splice(i, 0, regexResult["index"]); linkLength.splice(i, 0, str.length); break; } } // linkURL.push(str); // linkStart.push(regexResult["index"]); // linkLength.push(str.length); regexResult = regexSearch.exec(__text); //trace ("URL --> [" + str + "]"); } // Finally, add the links as HTML links var newText:String = ""; var lastPos:int = 0; i = 0; while (i < linkStart.length) { newText += __text.substr(lastPos, linkStart[i] - lastPos); newText += ""; newText += __text.substr(linkStart[i], linkLength[i]); newText += ""; lastPos = linkStart[i] + linkLength[i]; i++; } newText += __text.substr(lastPos); //trace ("--> " + newDescription); return newText; } public static function URLEncode(__text:String): String { __text = escape(__text); __text = __text.split("@").join("%40"); __text = __text.split("+").join("%2B"); __text = __text.split("/").join("%2F"); return __text; } public static function generateGUID(): String { // http://en.wikipedia.org/wiki/Globally_unique_identifier // This one is actually more unorthodox var i:int; var nums:Vector. = new Vector.(); nums.push(getUniqueSerialNumber()); nums.push(getUniqueSerialNumber()); for (i = 0; i < 10; i++) { nums.push(Math.round(Math.random() * 255)); } var strs:Vector. = new Vector.(); for (i = 0; i < nums.length; i++) { strs.push(("00" + nums[i].toString(16)).substr(-2,2)); } var now:Date = new Date(); var secs:String = ("0000" + now.getMilliseconds().toString(16)).substr(-4, 4); // 4-2-2-6 return strs[0]+strs[1]+strs[2]+strs[3]+"-"+secs+"-"+strs[4]+strs[5]+"-"+strs[6]+strs[7]+strs[8]+strs[9]+strs[10]+strs[11]; } public static function getUniqueSerialNumber(): int { return uniqueSerialNumber++; } public static function validate(__text:String, __expression:RegExp):Boolean { return __expression.test(__text); } } }