Skip to content

Instantly share code, notes, and snippets.

@jdonaldson
Created October 31, 2012 19:49
Show Gist options
  • Save jdonaldson/3989377 to your computer and use it in GitHub Desktop.
Save jdonaldson/3989377 to your computer and use it in GitHub Desktop.
Extended Html escape/unescape methods
class Escape{
/**
Escape HTML special characters of the string.
&, <, >, ", ', `, , !, @, $, %, (, ), =, +, {, }, [, and ]
**/
public static function htmlEscape( s : String ) : String {
return s.split("&").join("&amp;")
.split("<").join("&lt;")
.split(">").join("&gt;")
.split('"').join("&quot;")
.split("'").join("&#39;")
.split("`").join("&#96;")
.split("!").join("&#33;")
.split("@").join("&#64;")
.split("$").join("&#36;")
.split("%").join("&#37;")
.split("(").join("&#40;")
.split(")").join("&#41;")
.split("=").join("&#61;")
.split("+").join("&#43;")
.split("{").join("&#123;")
.split("}").join("&#125;")
.split("[").join("&#91;")
.split("]").join("&#93;")
.split(" ").join("&nbsp;");
}
/**
UnEscape HTML special characters of the string.
&, <, >, ", ', `, , !, @, $, %, (, ), =, +, {, }, [, and ]
**/
public static function htmlUnescape( s : String ) : String {
return s.split("&amp;").join("&")
.split("&lt;").join("<")
.split("&gt;").join(">")
.split("&quot;").join('"')
.split("&#39;").join("'")
.split("&#96;").join("`")
.split("&#33;").join("!")
.split("&#64;").join("@")
.split("&#36;").join("$")
.split("&#37;").join("%")
.split("&#40;").join("(")
.split("&#41;").join(")")
.split("&#61;").join("=")
.split("&#43;").join("+")
.split("&#123;").join("{")
.split("&#125;").join("}")
.split("&#91;").join("[")
.split("&#93;").join("]")
.split("&nbsp;").join(" ");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment