Skip to content

Instantly share code, notes, and snippets.

@geraldyeo
Created April 4, 2011 02:26
Show Gist options
  • Save geraldyeo/901060 to your computer and use it in GitHub Desktop.
Save geraldyeo/901060 to your computer and use it in GitHub Desktop.
various useful as3 functions
// tinting
var colorTransform : ColorTransform = bmp.transform.colorTransform;
colorTransform.color = 0x3b1c4d;
bmp.transform.colorTransform = colorTransform;
// get URL of page where swf is embedded
function get currentURL():String
{
var url:String;
if (ExternalInterface.available) {
return ExternalInterface.call("window.location.href");
}
return url;
}
// actionscript hates utf8
public static function encodeUTF8 (text:String):String {
var a:uint, n:uint, A:uint;
var utf:String;
utf = "";
A = text.length;
for (a = 0; a < A; a++) {
n = text.charCodeAt (a);
if (n < 128) {
utf += String.fromCharCode (n);
} else if ((n > 127) && (n < 2048)) {
utf += String.fromCharCode ((n >> 6) | 192);
utf += String.fromCharCode ((n & 63) | 128);
} else {
utf += String.fromCharCode ((n >> 12) | 224);
utf += String.fromCharCode (((n >> 6) & 63) | 128);
utf += String.fromCharCode ((n & 63) | 128);
}
}
return utf;
}
// PHP’s html_strip_tags
public static function stripHtmlTags(html:String, tags:String = ""):String
{
var tagsToBeKept:Array = new Array();
if (tags.length > 0)
tagsToBeKept = tags.split(new RegExp("\\s*,\\s*"));
var tagsToKeep:Array = new Array();
for (var i:int = 0; i < tagsToBeKept.length; i++)
{
if (tagsToBeKept[i] != null && tagsToBeKept[i] != "")
tagsToKeep.push(tagsToBeKept[i]);
}
var toBeRemoved:Array = new Array();
var tagRegExp:RegExp = new RegExp("<([^>\\s]+)(\\s[^>]+)*>", "g");
var foundedStrings:Array = html.match(tagRegExp);
for (i = 0; i < foundedStrings.length; i++)
{
var tagFlag:Boolean = false;
if (tagsToKeep != null)
{
for (var j:int = 0; j < tagsToKeep.length; j++)
{
var tmpRegExp:RegExp = new RegExp("<\/?" + tagsToKeep[j] + "( [^<>]*)*>", "i");
var tmpStr:String = foundedStrings[i] as String;
if (tmpStr.search(tmpRegExp) != -1)
tagFlag = true;
}
}
if (!tagFlag)
toBeRemoved.push(foundedStrings[i]);
}
for (i = 0; i < toBeRemoved.length; i++)
{
var tmpRE:RegExp = new RegExp("([\+\*\$\/])","g");
var tmpRemRE:RegExp = new RegExp((toBeRemoved[i] as String).replace(tmpRE, "\\$1"),"g");
html = html.replace(tmpRemRE, "");
}
return html;
}
/*
A slug is usually a few words with each word separated by a delimiting character, usually an underscore or hyphen. A slugified version of the string "10 Tips to a Better Life!" would be "10-tips-to-a-better-life".
*/
function slugify(string:String):String
{
const pattern1:RegExp = /[^\w- ]/g; // Matches anything except word characters, space and -
const pattern2:RegExp = / +/g; // Matches one or more space characters
var s:String = string;
return s.replace(pattern1, "").replace(pattern2, "-").toLowerCase();
}
// strip http
function stripHttp(string:String, stripWWW:Boolean = false):String
{
var s:String = string;
var regexp:RegExp = new RegExp(!stripWWW ? "https*:\/\/" : "https*:\/\/(www\.)*", "ig");
return s.replace(regexp, "");
}
// strip xml namespace
function stripXMLNamespaces(xml:XML):XML
{
var s:String = xml.toString();
var pattern1:RegExp = /\s*xmlns[^\'\"]*=[\'\"][^\'\"]*[\'\"]/gi;
s = s.replace(pattern1, "");
var pattern2:RegExp = /&lt;[\/]{0,1}(\w+:).*?&gt;/i;
while(pattern2.test(s)) {
s = s.replace(pattern2.exec(s)[1], "");
}
return XML(s);
}
// email validation
public static function validateEmail(email:String):Boolean {
var emailExpression:RegExp = /^[a-z][\w.-]+@\w[\w.-]+\.[\w.-]*[a-z][a-z]$/i;
return emailExpression.test(email);
}
// position objects in grid
for (var i:uint = 0; i < 20; i++)
{
var displayObject:MyDisplayObject = new MyDisplayObject();
displayObject.x = displayObject.width * ( i % 5 );
displayObject.y = displayObject.height * Math.floor( i / 5 );
addChild(displayObject);
}
// Round the Positional Values of a DisplayObjectContainer and its Children
function roundPositions(displayObjectContainer:DisplayObjectContainer):void
{
if (!(displayObjectContainer is Stage)) {
displayObjectContainer.x = Math.round(displayObjectContainer.x);
displayObjectContainer.y = Math.round(displayObjectContainer.y);
}
for (var i:uint = 0; i < displayObjectContainer.numChildren; i++) {
var child:DisplayObject = displayObjectContainer.getChildAt(i);
if (child is DisplayObjectContainer) {
roundPositions(child as DisplayObjectContainer);
} else {
child.x = Math.round(child.x);
child.y = Math.round(child.y);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment