开发者

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:    &lt;&gt;&amp;abcdef&copy;


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

< → &lt;

> → &gt;

' → &#39;

" → &quot;

& → &amp;

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&amp;def
php> echo htmlspecialchars("abc<>\"&def");
abc&lt;&gt;&quot;&amp;def
php> echo htmlspecialchars("abc<>\"&d'ef");
abc&lt;&gt;&quot;&amp;d'ef
php> echo htmlspecialchars("abc<   >\"&d'ef");
abc&lt;   &gt;&quot;&amp;d'ef
php> echo htmlspecialchars("abc def");
abc def
php>

Notes:

  1. Spaces and newlines are kept as-is.
  2. The defined entities are replaced once per instance.
  3. 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("&lt;"); break;
             case '>': sb.append("&gt;"); break;
             case '&': sb.append("&amp;"); break;
             case '"': sb.append("&quot;"); break;
             default:  sb.append(c); break;
          }
       }
       return sb.toString();
    }
}

Which prints:

eric@dev ~ $ java Main
'&amp;'
'&lt;'
'&gt;'
' '
'     '
'&amp;&lt;&gt;abc'
'abc&amp;&lt;&gt;'

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜