How to encode special chars in html content
In Java, Is there a third party sour开发者_高级运维ce available or quick command to convert html special chars in a string to html encoded content?
For example:
Original code: <>&abcdef ©
After encoding: <>&abcdef©
If you want to convert a string to HTML entities to test something quickly, you can use webservices like this one:
http://www.primitivetype.com/resources/htmlentities.php
[EDIT] For Java you can use the StringEscapeUtils from Apache Commons Lang. See this thread: Recommended method for escaping HTML in Java
import static org.apache.commons.lang.StringEscapeUtils.escapeHtml;
// ...
String source = "The less than sign (<) and ampersand (&) must be escaped before using them in HTML";
String escaped = escapeHtml(source);
I borrowed the example from the thread mentioned above.
This is old, but it doesn't have an accepted answer yet. This is my version with pure java:
public String toHTML(String str) {
String out = "";
for (char c: str.toCharArray()) {
if(!Character.isLetterOrDigit(c))
out += String.format("&#x%x;", (int)c);
else
out += String.format("%s", c);
}
return out;
}
Works great with html5 and utf-8.
Convert
< → <
> → >
' → '
" → "
& → &
Source of knowledge: https://www.php.net/manual/en/function.htmlspecialchars.php
Javascript Solution: Find working fiddle here: http://jsfiddle.net/ezmilhouse/Zb5C9/1/
===
Sample uses 2 functions borrowed from php.js:
get_html_translation_table()
https://github.com/kvz/phpjs/raw/master/functions/strings/get_html_translation_table.js
htmlentities()
https://github.com/kvz/phpjs/raw/master/functions/strings/htmlentities.js
Here is some java code to replicate PHP's default htmlspecialchars(str) as closely as possible:
First lets look at how PHP's htmlspecialchars(str)
works:
php> echo htmlspecialchars("abc\ndef");
abc
def
php> echo htmlspecialchars("abc&def");
abc&def
php> echo htmlspecialchars("abc<>\"&def");
abc<>"&def
php> echo htmlspecialchars("abc<>\"&d'ef");
abc<>"&d'ef
php> echo htmlspecialchars("abc< >\"&d'ef");
abc< >"&d'ef
php> echo htmlspecialchars("abc def");
abc def
php>
Notes:
- Spaces and newlines are kept as-is.
- The defined entities are replaced once per instance.
- Extra wingdings like copyright
©
and euro symbol€
are kept as-is.
The code:
public class Main{
public static void main(String[] args) {
System.out.println("'" + stringToHtmlString("&") + "'");
System.out.println("'" + stringToHtmlString("<") + "'");
System.out.println("'" + stringToHtmlString(">") + "'");
System.out.println("'" + stringToHtmlString(" ") + "'");
System.out.println("'" + stringToHtmlString(" ") + "'");
System.out.println("'" + stringToHtmlString("&<>abc") + "'");
System.out.println("'" + stringToHtmlString("abc&<>") + "'");
}
public static final String stringToHtmlString(String s){
StringBuffer sb = new StringBuffer();
int n = s.length();
for (int i = 0; i < n; i++) {
char c = s.charAt(i);
switch (c) {
case '<': sb.append("<"); break;
case '>': sb.append(">"); break;
case '&': sb.append("&"); break;
case '"': sb.append("""); break;
default: sb.append(c); break;
}
}
return sb.toString();
}
}
Which prints:
eric@dev ~ $ java Main
'&'
'<'
'>'
' '
' '
'&<>abc'
'abc&<>'
It's not a perfect clone of PHP's htmlspecialchars(str)
function, but it's close enough for me.
Here's another which does html entity conversions for obfuscated character sets: http://www.rgagnon.com/javadetails/java-0306.html
精彩评论