Created
August 22, 2015 10:28
-
-
Save OsandaMalith/1bb872aa3faad7ee650b to your computer and use it in GitHub Desktop.
HTML Encode function for Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HTMLEncode { | |
public static void main(String[] args) { | |
System.out.println(HTMLEncode("Hi osanda \"><h1>XSS</h1>")); | |
} | |
public static String HTMLEncode(String s) { | |
StringBuilder out = new StringBuilder(); | |
for (int i = 0; i < s.length(); i++) { | |
char c = s.charAt(i); | |
out.append(c > 0x7f || c == '"' || c == '&' || c == '<' || c == '>' ? "&#" + (int) c + ";" : c); | |
} | |
return out.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment