Java Utililty Methods HTML Filter

List of utility methods to do HTML Filter

Description

The list of methods to do HTML Filter are organized into topic(s).

Method

StringfilterHtml(String body)
filter Html
if (body == null) {
    return "";
body = body.replace(" ", "&nbsp;").replace("\r", "<BR/>").replace("\n", "<BR/>").replaceAll("<", "&lt;")
        .replaceAll(">", "&gt;").replaceAll("\"", "&quot;").replaceAll("'", "&quot;");
return body;
StringfilterHtml(String input)
* Remove HTML from input string.
String out = input.replaceAll("<.*?>", "");
out = out.replaceAll(" +", " ");
out = out.replaceAll(",(\\S)", ", $1");
return out;
StringfilterHTML(String input)
filter HTML
String result = (input == null ? "" : input);
if (input != null && input.length() > 0) {
    StringBuffer filtered = new StringBuffer(input.length());
    char c;
    for (int i = 0; i < input.length(); i++) {
        c = input.charAt(i);
        String s = null;
        switch (c) {
...
StringfilterHTML(String input)
filter HTML
StringBuffer filtered = new StringBuffer(input.length());
char c;
for (int i = 0; i < input.length(); i++) {
    c = input.charAt(i);
    if (c == '<') {
        filtered.append("&lt;");
    } else if (c == '>') {
        filtered.append("&gt;");
...
StringfilterHTML(String s)
Filters out all HTML tags.
if (s == null) {
    return null;
s = s.replaceAll("&lt;", "");
s = s.replaceAll("&gt;", "");
s = s.replaceAll("&nbsp;", "");
s = s.replaceAll("(?s)<[Ss][Cc][Rr][Ii][Pp][Tt].*?>.*?</[Ss][Cc][Rr][Ii][Pp][Tt]>", "");
s = s.replaceAll("(?s)<[Ss][Tt][Yy][Ll][Ee].*?>.*?</[Ss][Tt][Yy][Ll][Ee]>", "");
...
StringfilterHtml(String s)
filter Html
if (s == null) {
    return null;
s = s.replaceAll("&lt;", "");
s = s.replaceAll("&gt;", "");
s = s.replaceAll("&nbsp;", "");
s = s.replaceAll("(?s)<!--.*?-->", "");
return s.replaceAll("(?s)<.*?>", "");
...
StringfilterHtmlAndTruncate(String s)
filter Html And Truncate
return filterHtmlAndTruncate(s, DEFAULT_MAX_CONTENT_LENGTH);
StringfilterHtmlAndTruncate(String s)
filter Html And Truncate
return filterHtmlAndTruncate(s, MAX_CONTENT_LENGTH);
StringfilterHtmlTag(String body)
filter Html Tag
if (body == null) {
    return "";
body = body.replaceAll("FRAME", "");
body = body.replaceAll("frame", "");
body = body.replaceAll("Frame", "");
body = body.replaceAll("APPLET", "");
body = body.replaceAll("applet", "");
...
StringfilterHtmlTag(String content)
filter Html Tag
boolean flag = true;
StringBuffer stringBuffer = new StringBuffer(2048);
for (int i = 0; i < content.length(); i++) {
    if (content.charAt(i) == '<') {
        flag = false;
        continue;
    if (content.charAt(i) == '>') {
...