/**
* Encode text to html.
*/
public static String encodeHtml(String html) {
StringBuilder sb = new StringBuilder(html.length());
for(int i=0; i<html.length(); i++) {
char c = html.charAt(i);
switch(c) {
case ' ':
sb.append(" ");
break;
case '&':
sb.append("&");
break;
case '<':
sb.append("<");
break;
case '>':
sb.append(">");
break;
case '/n':
sb.append("<br/>");
break;
case '/r':
break;
//case '/'':
// sb.append("'");
// break;
case '/"':
sb.append(""");
break;
default:
sb.append(c);
}
}
return sb.toString();
}
Encode text to html.
HTML文本转义实用方法
最新推荐文章于 2025-01-22 15:21:43 发布
本文介绍了一种将HTML文本进行转义处理的方法,通过使用StringBuilder遍历字符串并替换特殊字符来实现。此方法能够有效避免HTML注入等问题,确保网页内容的安全显示。
1410

被折叠的 条评论
为什么被折叠?



