2006年6月21日 星期三

On the Stre@m - Java

On the Stre@m - Java: "JSP/Java - strip_tags() PHP like function

Another PHP function that is very used is strip_tags.
This function tries to return a string with all HTML tags stripped from a given string.

public static String strip_tags(String text, String allowedTags) {
String[] tag_list = allowedTags.split(',');
Arrays.sort(tag_list);

final Pattern p = Pattern.compile('<[/!]?([^s>]*)s*[^>]*>');
Matcher m = p.matcher(text);

StringBuffer out = new StringBuffer();
int lastPos = 0;
while (m.find()) {
String tag = m.group(1);
// if tag not allowed: skip it
if(Arrays.binarySearch(tags, tag) < lastpos =" m.end();"> 0) {
out.append(text.substring(lastPos));
return out.toString().trim();
} else {
return text;
}
}"
張貼留言