The following is a handy Java utility method to encode a plain text string into appropriate HTML entities. This method is similar to the htmlentities
function in PHP. This method only encodes some of the more common characters to the equivalent HTML entity name while the rest are encoded as HTML entity numbers.
public static String stringToHTMLEntities(String string) { if (string == null) { return ""; } StringBuffer sb = new StringBuffer(string.length()); boolean lastWasBlankChar = false; int len = string.length(); char c; for (int i = 0; i < len; i++) { c = string.charAt(i); if (c == ' ') { if (lastWasBlankChar) { lastWasBlankChar = false; sb.append(" "); } else { lastWasBlankChar = true; sb.append(' '); } } else { lastWasBlankChar = false; if (c == '"') { sb.append("\""); } else if (c == '&') { sb.append("&"); } else if (c == '<') { sb.append("<"); } else if (c == '>') { sb.append(">"); } else if (c == 'n') { sb.append("n"); } else if (c == 39) { //check for apostrophe character sb.append("'"); } else { int ci = 0xffff & c; if (ci < 160) { sb.append(c); } else { sb.append("&#"); sb.append(new Integer(ci).toString()); sb.append(';'); } } } } return sb.toString(); }