java解析ubb2html

package com.ztx.club.util; import java.io.UnsupportedEncodingException; import java.util.Hashtable; import java.util.regex.Matcher; import java.util.regex.Pattern; import com.ztx.util.web.ParamUtils; public class StringUtils { private static final String[] regexUBB = new String[] { "\\[/url\\]", "\\[/email\\]", "\\[/color\\]", "\\[/size\\]", "\\[/font\\]", "\\[/align\\]", "\\[b\\]", "\\[/b\\]", "\\[i\\]", "\\[/i\\]", "\\[u\\]", "\\[/u\\]", "\\[list\\]", "\\[list=1\\]", "\\[list=a\\]", "\\[list=A\\]", "\\[\\*\\]", "\\[/list\\]", "\\[indent\\]", "\\[/indent\\]", "\\[code\\]", "\\[/code\\]", "\\[quote\\]", "\\[/quote\\]", "\\[table\\]", "\\[tr\\]", "\\[td\\]", "\\[/tr\\]", "\\[/td\\]", "\\[/table\\]" }; private static final String[] replacementUBB = new String[] { "", "", "", "", "", "

", " ", "", " ", "", " ", "", "
  • ", "
  1. ", "
    1. ", "
      1. ", "
      2. ", "
", "
", "
", "
<textarea name="codes" id="codes" rows="12" cols="65">", "</textarea>
<input type="button" value="运行代码" οnclick="RunCode()"> <input type="button" value="复制代码" οnclick="CopyCode()"> <input type="button" value="另存代码" οnclick="SaveCode()">  提示:您可以先修改部分代码再运行
", "
", "
", "", "", "", "", "
", "
" }; private static final String[] replacementHTML = new String[] { "[/align]", "[b]", "[/b]", "[i]", "[/i]", "[u]", "[/u]", "[list]", "[list=1]", "[list=a]", "[list=A]", "[*]", "[/list]", "[indent]", "[/indent]", "[code]", "[/code]", "[quote]", "[/quote]", "[table]", "[tr]", "[td]", "[/tr]", "[/td]", "[/table]" }; private static final String[] regexHTML = new String[] { "", " ", "", " ", "", " ", "", "
  • ", "
  1. ", "
    1. ", "
      1. ", "
      2. ", "
", "
", "
", "
<textarea name="codes" id="codes" rows="12" cols="65">", "</textarea>
<input type="button" value="运行代码" οnclick="RunCode\\(\\)"> <input type="button" value="复制代码" οnclick="CopyCode\\(\\)"> <input type="button" value="另存代码" οnclick="SaveCode\\(\\)">  提示:您可以先修改部分代码再运行
", "
", "
", "", "", "", "", "
", "
" }; public static String parsetable(String width,String bgcolor,String str) { if(width==null ) { width="90%"; } else { width = width.trim().endsWith("%")?( Integer.parseInt(width.substring(0, width.length() - 1)) <= 98 ? width :"98%"):(Integer.parseInt(width) <= 560 ? width : "98%"); } if(bgcolor==null){ bgcolor="#000"; } str = str.replaceAll("\\[tr(?:=([\\(\\)%,#\\w]+))?\\]\\s*\\[td(?:=(\\d{1,2}),(\\d{1,2})(?:,(\\d{1,4}%?))?)?\\]", ""+"" ); str = str.replaceAll("\\[\\/td\\]\\s*\\[td(?:=(\\d{1,2}),(\\d{1,2})(?:,(\\d{1,4}%?))?)?\\]", "" ); str = str.replaceAll("\\[\\/td\\]\\s*\\[\\/tr\\]", ""); return " $2"); // ubb = ubb.replaceAll("\\[url=([^\\]]*)\\]", // " "); // // 邮箱UBB // ubb = ubb // .replaceAll("(\\[email\\])([^\\]]*)(\\[/email\\])", // " $2"); // ubb = ubb // .replaceAll( // "\\[email=([^\\]]*)\\]", // " email1.gif "); // // QQ号码UBB // ubb = ubb // .replaceAll( // "\\[qq=([1-9]*)\\]([1-9]*)\\[/qq\\]", // " 点击这里给我发消息"); // // 颜色UBB // ubb = ubb.replaceAll("\\[color=([^\\]]*)\\]", " "); // // 文字字体UBB // ubb = ubb.replaceAll("\\[font=([^\\]]*)\\]", ""); // // 文字大小UBB // ubb = ubb.replaceAll("\\[size=([1-7])\\]", ""); // // 文字对齐方式UBB // ubb = ubb.replaceAll("\\[align=(center|left|right)\\]", // "
"); // // 表格UBB // ubb = ubb.replaceAll("\\[table=([^\\]]*)\\]", ""); // // FLASH动画UBB // ubb = ubb // .replaceAll( // "(\\[flash\\])(http://[^\\]]*(.swf))(\\[/flash\\])", // "$2"); // ubb = ubb // .replaceAll( // "(\\[flash=*([0-9]*),*([0-9]*)\\])(http://[^\\]]*(.swf))(\\[/flash\\])", // "$4"); // // MEDIA PLAY播放UBB // ubb = ubb // .replaceAll( // "\\[wmv\\]([^\\]]*)\\[/wmv]", // ""); // ubb = ubb // .replaceAll( // "\\[wmv=*([0-9]*),*([0-9]*)\\]([^\\]]*)\\[/wmv]", // ""); // // REALPLAY 播放UBB // ubb = ubb // .replaceAll( // "\\[rm\\]([^\\]]*)\\[/rm]", // "
"); // ubb = ubb // .replaceAll( // "\\[rm=*([0-9]*),*([0-9]*)\\]([^\\]]*)\\[/rm]", // "
"); // // for (int i = 0; i < regexUBB.length; i++) // ubb = ubb.replaceAll(regexUBB[i], replacementUBB[i]); // // return ubb; // } // // public static String escapeHTML2UBB(String html) { // // 图片html // html = html // .replaceAll( // " 按此在新窗口浏览图片", // "[img]$1://$2[/img]"); // // 链接html // html = html.replaceAll( // " (.*)", // "[url=$1://$2]$3[/url]"); // // 邮箱html // html = html // .replaceAll( // " email1.gif (.*)", // "[email=$1]$2[/email]"); // // QQ号码html // html = html // .replaceAll( // " 点击这里给我发消息", // "[qq=$1]$1[/qq]"); // // html = html // .replaceAll( // " (.*)(.*)(.*)(.*)(.*)", // "[$1=$2]$3[$4=$5]$6[$7=$8]$9[/$7]$10[/$4]$11[/$1]"); // // html = html // .replaceAll( // " (.*)(.*)(.*)", // "[$1=$2]$3[$4=$5]$6[/$4]$7[/$1]"); // // html = html.replaceAll(" (.*)", // "[$1=$2]$3[/$1]"); // // html = html.replaceAll("\\[face=([^\\]]*)\\](.*)\\[/face\\]", // "[font=$1]$2[/font]"); // // // 文字对齐方式html // html = html.replaceAll("
", // "[align=$1]"); // // 表格html // html = html.replaceAll("
", "[table=$1]"); // // FLASH动画html // html = html // .replaceAll( // "([^']*)", // "[flash]$1[/flash]"); // html = html // .replaceAll( // "([^']*)", // "[flash=$1,$2]$3[/flash]"); // // MEDIA PLAY播放html // html = html // .replaceAll( // "", // "[wmv]$1[/wmv]"); // html = html // .replaceAll( // "", // "[wmv=$1,$2]$3[/wmv]"); // // REALPLAY 播放html // html = html // .replaceAll( // "
", // "[rm]$1[/rm]"); // html = html // .replaceAll( // "
", // "[rm=$1,$2]$3[/rm]"); // // for (int i = 0; i < regexHTML.length; i++) // html = html.replaceAll(regexHTML[i], replacementHTML[i]); // // return html; // } public static String escapeUBB2HTML(String ubb) { ubb = ParamUtils.escapeHTMLTags(ubb); ubb=ubb.trim().toLowerCase(); if(ubb.trim().equals("")){ return ""; } // 链接UBB ubb=ubb.replaceAll("\\[url\\]\\s*(www.|https?:\\/\\/|ftp:\\/\\/|gopher:\\/\\/|news:\\/\\/|telnet:\\/\\/|rtsp:\\/\\/|mms:\\/\\/|callto:\\/\\/|bctp:\\/\\/|ed2k:\\/\\/){1}([^\\[\"\']+?)\\s*\\[\\/url\\]", " "); ubb= ubb.replaceAll("\\[url=www.([^\\[\"\']+?)\\](.+?)\\[\\/url\\]", " $2"); ubb= ubb.replaceAll("\\[url=(https?|ftp|gopher|news|telnet|rtsp|mms|callto|bctp|ed2k){1}:\\/\\/([^\\[\"\']+?)\\]([\\s\\S]+?)\\[\\/url\\]", " $3"); //字体 ubb = ubb.replaceAll("\\[color=([^\\[\\<]+?)\\]", " "); ubb = ubb.replaceAll("\\[size=(\\d+?)\\]", ""); ubb = ubb.replaceAll("\\[size=(\\d+(\\.\\d+)?(px|pt|in|cm|mm|pc|em|ex|%)+?)\\]", ""); ubb = ubb.replaceAll("\\[font=([^\\[\\<]+?)\\]", ""); ubb = ubb.replaceAll("\\[align=([^\\[\\<]+?)\\]", "

"); //span ubb = ubb.replaceAll("\\[float=([^\\[\\<]+?)\\]", "
"); //table ubb = tableStr(ubb); //picture ubb = ubb.replaceAll("\\[img\\]\\s*([^\\[\\<\\r\\n]+?)\\s*\\[\\/img\\]", "\"$1\""); ubb = ubb.replaceAll("\\[img=(\\d{1,4})[x|\\,](\\d{1,4})\\]\\s*([^\\[\\<\\r\\n]+?)\\s*\\[\\/img\\]", "\"\""); ubb = ubb.replaceAll("(\\[flash\\])(http://[^\\]]*(.swf))(\\[/flash\\])","$2"); ubb = ubb.replaceAll("(\\[flash=*([0-9]*),*([0-9]*)\\])(http://[^\\]]*(.swf))(\\[/flash\\])","$4"); // 替换不匹配标签 for (int i = 0; i < regexUBB.length; i++) ubb = ubb.replaceAll(regexUBB[i], replacementUBB[i]); return ubb; } public static String tabletag(String attributes) { String width = ""; String re = "width=([\"\']?)(\\d{1,4}%?)(\\1)"; Pattern pattern=Pattern.compile(re); Matcher matches=pattern.matcher(attributes); if(matches!= null) { while(matches.find()) { width = matches.group(2).trim().endsWith("%")? (Integer.parseInt(matches.group(2).substring(0,matches.group(2).length()- 1)) <= 98 ? matches.group(2) : "98%") : (Integer.parseInt(matches.group(2))<= 560 ? matches.group(2): "98%"); } } else { re = "width\\s?:\\s?(\\d{1,4})([px|%])"; Pattern pattern1=Pattern.compile(re); Matcher matches1=pattern1.matcher(attributes); while(matches1.find()){ if(matches1 != null) { width = matches1.group(2) == "%" ? (Integer.parseInt(matches1.group(1))<= 98 ? matches1.group(1) : "98%") : (Integer.parseInt(matches1.group(1)) <= 560 ? matches1.group(1) : "98%"); } } } String bgcolor =""; re ="(?:background|background-color|bgcolor)[:=]\\s*([\"\']?)((rgb\\(\\d{1,3}%?,\\s*\\d{1,3}%?,\\s*\\d{1,3}%?\\))|(#[0-9a-fA-F]{3,6})|([a-zA-Z]{1,20}))(\\1)"; Pattern pattern2=Pattern.compile(re); Matcher matches2=pattern2.matcher(attributes); while(matches2.find()){ if(matches2 != null) { bgcolor = matches2.group(2); width = (width!=null&&!width.trim().equals("")) ? width : "98%"; } } String temp=""; if(bgcolor!=null&&!bgcolor.trim().equals("")&&width!=null&&!width.trim().equals("")){ temp="[table=" + width + "," + bgcolor + "]" ; }else if(bgcolor!=null&&!bgcolor.trim().equals("")&&width==null) { temp="[table=" + bgcolor + "]" ; }else if(width!=null&&!width.trim().equals("")&&bgcolor==null) { temp="[table=" + width + "]" ; }else if(width==null&&bgcolor==null) { temp="[table]" ; } return temp; } public static String tdtag(String attributes) { int colspan = 1; int rowspan = 1; int width = 0; String re = "colspan=([\"\']?)(\\d{1,2})(\1)"; Pattern pattern=Pattern.compile(re); Matcher matches=pattern.matcher(attributes); while(matches.find()){ if(matches!= null) { colspan =Integer.parseInt( matches.group(2)); } } re ="rowspan=([\"\']?)(\\d{1,2})(\\1)"; Pattern pattern1=Pattern.compile(re); Matcher matches1=pattern1.matcher(attributes); while(matches1.find()){ if(matches1!= null) { rowspan = Integer.parseInt(matches1.group(2)); } } re = "width=([\"\']?)(\\d{1,4}%?)(\\1)"; Pattern pattern2=Pattern.compile(re); Matcher matches2=pattern2.matcher(attributes); while(matches2.find()){ if(matches2 != null) { width = Integer.parseInt(matches2.group(2)); } } return width==0 ? (colspan == 1 && rowspan == 1 ? "[td]" : "[td=" + colspan + "," + rowspan + "]") : "[td=" + colspan + "," + rowspan +"," + width + "]"; } public static String tableH2U(String html){ if(html==null&&html.trim().equals("")) return ""; Pattern pattern=Pattern.compile("<table([^>]*(width|background|background-color|bgcolor)[^>]*)>"); Matcher ma=pattern.matcher(html); String temp=""; while(ma.find()){ temp=tabletag(ma.group(1)).toString(); } html=html.replaceAll("<table([^>]*(width|background|background-color|bgcolor)[^>]*)>", temp); return html; } public static String tdH2U(String html){ if(html==null&&html.trim().equals("")) return ""; Pattern pattern=Pattern.compile("<t[dh]([^>]*(width|colspan|rowspan)[^>]*)>"); Matcher ma=pattern.matcher(html); String temp=""; while(ma.find()){ temp=tdtag(ma.group(2)).toString(); } html=html.replaceAll("<table([^>]*(width|background|background-color|bgcolor)[^>]*)>", temp); return html; } public static String imgtag(String attributes) { int width =0; int height =0; String src=""; String re = "src=([\"\']?)([\\s\\S]*?)(\\1)"; Pattern pattern=Pattern.compile(re); Matcher matches =pattern.matcher(attributes); while(matches.find()){ if(matches != null) { src= matches.group(2); } else { return ""; } } re = "width\\s?:\\s?(\\d{1,4})px"; Pattern pattern1=Pattern.compile(re); Matcher matches1 =pattern1.matcher(attributes); while(matches1.find()){ if(matches1 != null) { width = Integer.parseInt(matches1.group(1)); } } re = "height\\s?:\\s?(\\d{1,4})px"; Pattern pattern2=Pattern.compile(re); Matcher matches2 =pattern2.matcher(attributes); while(matches2.find()){ if(matches2 != null) { height = Integer.parseInt(matches2.group(1)); } } if(width==0 || height==0) { re = "width=([\"\']?)(\\d+)(\\1)"; Pattern pattern3=Pattern.compile(re); Matcher matches3 =pattern3.matcher(attributes); while(matches3.find()){ if(matches3 != null) { width = Integer.parseInt(matches3.group(2)); } } re = "height=([\"\']?)(\\d+)(\\1)"; Pattern pattern4=Pattern.compile(re); Matcher matches4 =pattern4.matcher(attributes); while(matches4.find()){ if(matches4 != null) { height = Integer.parseInt(matches4.group(2)); } } } re = "aid=([\"\']?)attach_(\\d+)(\\1)"; String imgtag = "img"; Pattern pattern5=Pattern.compile(re); Matcher matches5 =pattern5.matcher(attributes); while(matches5.find()){ if(matches5 != null) { imgtag = "localimg"; return "[attachimg]" + matches.group(2) + "[/attachimg]"; } } return width > 0 && height > 0 ?"[" + imgtag +"=" + width + "," + height + "]" + src + "[/" + imgtag + "]" :"[img]" + src + "[/img]"; } public static String imgH2U(String html){ if(html==null&&html.trim().equals("")) return ""; String re="<img([^>]*src[^>]*)>"; Pattern pattern=Pattern.compile(re); Matcher matches =pattern.matcher(html); String temp=""; while(matches.find()){ if(matches != null) { temp=imgtag(matches.group(1)); html=html.replaceFirst("<img([^>]*src[^>]*)>", temp); } } return html; } public static String objectH2U(String html){ if(html==null&&html.trim().equals("")) return ""; String re="<object([^>]*src[^>]*)>"; Pattern pattern=Pattern.compile(re); Matcher matches =pattern.matcher(html); String temp=""; while(matches.find()){ if(matches != null) { temp=imgtag(matches.group(1)); html=html.replaceAll("<object([^>]*src[^>]*)>", temp); } } return html; } public static String recursion(String tagname,String text,String dofunction,String extraargs) { if(extraargs == null) { extraargs = ""; } int beginsearchpos = 0; int tagbegin = 0; do { tagname = tagname.toLowerCase(); String open_tag = "<" + tagname; int open_tag_len = open_tag.length(); String close_tag = ""; int close_tag_len = close_tag.length(); String textlower = text.trim().toLowerCase(); tagbegin = textlower.indexOf(open_tag, beginsearchpos); int strlen = text.length(); if(tagbegin == -1) { break; } int inquote =0; boolean found = false; int tagnameend = 0; int optionend = 0; char t_char =' '; for(optionend = tagbegin; optionend < strlen; optionend++) { t_char=text.charAt(optionend) ; inquote+=1; if(t_char == '>') { found=true; break; } } if(found==false) { break; } int offset = inquote + (tagbegin); //System.out.println("tagoptions"+"offset------"+offset+"start----"+(tagbegin * 1+1)+"end---"+(tagnameend - tagbegin)); String tagoptions = text.substring(tagbegin + open_tag_len, offset); String acttagname = textlower.trim().substring(tagbegin * 1 + 1, tagbegin*1+open_tag_len); //System.out.println("acttagname-----"+acttagname+"tagname-----"+tagname); if(!acttagname.trim().equals(tagname)) { beginsearchpos = inquote + (tagbegin);; continue; } int tagend = textlower.indexOf(close_tag, optionend); if(tagend == -1) { break; } int localbegin = tagbegin + inquote; //System.out.println("tagend"+tagend+"localbegin"+localbegin+"tagend - localbegin"+(tagend - localbegin)); String localtext =""; if(dofunction.trim().equals("simpletag")){ localtext=simpletag(tagoptions, text.substring(localbegin, tagend), tagname, extraargs); }else if(dofunction.trim().equals("atag")){ localtext=atag(tagoptions, text.substring(localbegin, tagend)); //System.out.println("localtext----"+localtext); }else if(dofunction.trim().equals("fonttag")){ localtext=fonttag(tagoptions, text.substring(localbegin, tagend)); } text = text.substring(0, tagbegin) + localtext + text.substring(tagend+close_tag_len); //System.out.println("text----"+text); beginsearchpos = tagbegin + localtext.length()-1; } while(tagbegin!=-1); return text; } public static String simpletag(String options,String text,String tagname,String parseto) { if(text.trim() =="") { return ""; } text = recursion(tagname, text, "simpletag", parseto); return "[" + parseto + "]" + text + "[/" + parseto + "]"; } public static String getoptionvalue(String option,String text) { String re = option + "(\\s+?)?\\=(\\s+?)?[\"\']?(.+?)([\"\']|$|>)"; Pattern pattern=Pattern.compile(re); Matcher matches =pattern.matcher(text); String temp=""; while(matches.find()){ if(matches!= null) { temp= matches.group(3); } } if(temp.trim().equals("")){ return ""; }else{ return temp; } } public static String atag(String aoptions,String text) { if(text.trim() =="") { return ""; } String href = getoptionvalue("href", aoptions); String tag=""; if(href.length()>11&&href.substring(0, 11) == "javascript:") { return recursion("a", text, "atag","").trim(); } else if(href.length()>7&&href.substring(0, 7) == "mailto:") { tag = "email"; href = href.substring(7); } else { tag = "url"; } return "[" + tag + "=" + href + "]" + recursion("a", text, "atag","").trim()+ "[/" + tag + "]"; } public static String fonttag(String fontoptions,String text) { String prepend = ""; String append = ""; String[] tags = new String[] {"font"+" :"+ "face=", "size" +":"+ "size=", "color" +":"+ "color="}; for(int i=0;i<tags.length;i++) {="" string="" optionvalue="fetchoptionvalue(tags[i]," fontoptions);="" system.out.println("optionvalue---"+optionvalue);="" if(optionvalue!="null&amp;&amp;!optionvalue.trim().equals(&quot;&quot;))" {="" prepend="" +="[" +="" tags[i]="" +="" "=" + optionvalue + " ]";="" append="[/" +="" tags[i]="" +="" "]"="" +="" append;="" }="" }="" hashtable="" pend="parsestyle(fontoptions," prepend,="" append);="" system.out.println("prepend---"+pend.get("prepend")+"--append--"+pend.get("append"));="" return="" pend.get("prepend")="" +="" recursion("font",="" text,="" "fonttag","")="" +="" pend.get("append");="" }="" 查询字符串位置="" public="" static="" int="" strpos(string="" haystack,string="" needle,int="" offset)="" {="" int="" index="haystack.toLowerCase().indexOf(needle.toLowerCase()," offset);="" return="" index="=" -1="" ?="" -1="" :index;="" }="" public="" static="" string="" fetchoptionvalue(string="" option,string="" text)="" {="" int="" position="0;" if((="" position="strpos(text," option,0))=""> -1) { int delimiter = position + option.length(); char delimchar=' '; if(text.charAt(delimiter) == '\"') { delimchar = '\"'; } else if(text.charAt(delimiter) == '\'') { delimchar = '\''; } else { delimchar = ' '; } int delimloc = strpos(text, delimchar+"", delimiter + 1); if(delimloc ==-1) { delimloc = text.length(); } else if(delimchar == '\"' || delimchar == '\'') { delimiter++; } return text.substring(delimiter, delimloc - delimiter).trim(); } else { return ""; } } public static Hashtable parsestyle(String tagoptions,String prepend,String append) { Hashtable hash=new Hashtable(); String[][] searchlist = { {"align", "true", "text-align:\\s*(left|center|right);?"," 1"}, {"float", "true", "float:\\s*(left|right);?", "1"}, {"color","true", "^(?:\\s|)color:\\s*([^;]+);?", "1"}, {"font", "true", "font-family:\\s*([^;]+);?", "1"}, {"size","true", "font-size:\\s*(\\d+(\\.\\d+)?(px|pt|in|cm|mm|pc|em|ex|%|));?", "1"}, {"b", "false", "font-weight:\\s*(bold);?"}, {"i", "false", "font-style:\\s*(italic);?"}, {"u", "false", "text-decoration:\\s*(underline);?"} }; String style = getoptionvalue("style", tagoptions); String re = "^(?:\\s|)color:\\s*rgb\\((\\d+),\\s*(\\d+),\\s*(\\d+)\\)(;?)"; Pattern pattern=Pattern.compile(re); Matcher ma=pattern.matcher(style); while(ma.find()){ style="color:#"+Integer.parseInt(ma.group(2))+ma.group(3)+ma.group(4)+ma.group(5); } //style = style.replace(re, function($1, $2, $3, $4, $5) {return(+ parseInt($2).toString(16) + parseInt($3).toString(16) + parseInt($4).toString(16) + $5);}); int len = searchlist.length; for(int i = 0; i < len; i++) { Pattern pattern1=Pattern.compile(searchlist[i][2]); Matcher matcher=pattern1.matcher(style); while(matcher.find()) { if(matcher != null) { String opnvalue = matcher.group(Integer.parseInt(searchlist[i][3])); prepend += "[" + searchlist[i][0] + (searchlist[i][1] == "true" ? "=" + opnvalue + "]" : "]"); append = "[/" + searchlist[i][0] + "]" + append; } } } hash.put("prepend", prepend); hash.put("append", append); return hash; } public static String escapeHTML2UBB(String html) { //去标签去特殊符号 html=html.trim().toLowerCase(); String[] preg=new String[]{"<style.*?>[\\s\\S]*?<\\/style>","<noscript.*?>[\\s\\S]*?<\\/script>","<noscript.*?>[\\s\\S]*?<\\/noscript>","<select.*?>[\\s\\S]*?<\\/select>", "", "on[a-zA-Z]{3,16}\\s?=\\s?\"[\\s\\S]*?\""}; for(int i=0,size=preg.length;i<size;i++){ html="html.replaceAll(preg[i]," "");="" }="" html="html.replaceAll(&quot;(\\r\\n|\\n|\\r)&quot;," "");="" html="html.replaceAll(&quot;&amp;((#(32|127|160|173))|shy|nbsp);&quot;,&quot;&quot;).trim();" 转表格="" html="tableH2U(html);" html="html.replaceAll(&quot;" <table[^="">]*>","[table]"); html=html.replaceAll("<tr[^>]*(?:background|background-color|bgcolor|class)[:=]\\s*([\"\']?)([\\(\\)%,#\\w]+)(\\1)[^>]*>", "[tr=$3]"); html=html.replaceAll("<tr[^>]*>", "[tr]"); html=tdH2U(html); String[] tablepreg=new String[]{"<t[dh][^>]*>","<\\/t[dh]>","<\\/tr>","<\\/table>"}; String[] tableReplace=new String[]{"[td]", "[/td]", "[/tr]", "[/table]"}; for(int j=0,max=tablepreg.length;j<max;j++) {="" html="html.replaceAll(tablepreg[j]," tablereplace[j]);="" }="" 转字体大小="" html="html.replaceAll(&quot;&lt;h([0-9]+)[^">]*>(.*)<\\/h\\1>", "[size=$1]$2[/size]\\n\\n"); html = imgH2U(html); html = objectH2U(html); html = html.replaceAll("<a\\s+?name=([\"\']?)(.+?)(\\1)[\\s\\s]*?>([\\s\\S]*?)<\\/a>", "$4"); html = html.replaceAll("<br\\s+?style=([\"\']?)clear: both;?(\\1)[^\\="">]*>", ""); html = html.replaceAll("<br[^\\>]*>", "\n"); //遍历内容,替换标签。 html = recursion("b", html, "simpletag", "b"); html = recursion("htmlong", html, "simpletag", "b"); html = recursion("i", html, "simpletag", "i"); html = recursion("em", html, "simpletag", "i"); html = recursion("u", html, "simpletag", "u"); html = recursion("a", html, "atag",""); html = recursion("font", html, "fonttag",""); html = recursion("blockquote", html, "simpletag", "indent"); // FLASH动画html html = html.replaceAll("([^']*)","[flash]$1[/flash]"); html = html.replaceAll("([^']*)","[flash=$1,$2]$3[/flash]"); for (int i = 0; i < regexHTML.length; i++) html = html.replaceAll(regexHTML[i], replacementHTML[i]); html = html.replaceAll("<[\\/\\!]*?[^<>]*?>", ""); //System.out.println("html----"+html); return html; } // html代码转化 把一些html里的标签转化为特殊字符 public static String escapeHTMLTags(String input) { // Check if the string is null or zero length -- if so, return // what was sent in. if (input == null || input.length() == 0) { // return input; input = ""; } // Use a StringBuffer in lieu of String concatenation -- it is // much more efficient this way. StringBuilder buf = new StringBuilder(input.length()); char ch = ' '; for (int i = 0; i < input.length(); i++) { ch = input.charAt(i); if (ch == '<') { buf.append("<"); } else if (ch == '>') { buf.append(">"); } else if (ch == '"') { buf.append("'"); } else { buf.append(ch); } } return buf.toString(); } // 路径转换 public static String escapePath(String input) { if (input != null) { return input.replace("\\", "/"); } else { return ""; } } // 编码转换 public static String StrEnCoding(String str) { try { return new String(str.getBytes("ISO8859_1"), "gbk"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); return ""; } } // 截取字符串 public static String StrIntercept(String str, int length) { if (str.length() < length) { return str; } else { return str.substring(0, length) + "..."; } } // 判断字符串是可以转换为长整形 public static boolean isLong(String value) { try { Long.parseLong(value); return true; } catch (NumberFormatException e) { return false; } } // 发帖的时候除去某些标签 textarea script iframe public static String removeTag(String context) { if (context != null) { String content = context; content = content.trim().toLowerCase(); if (content.indexOf("οnlοad=autoresize(this)") > -1) { content = content .replaceAll("οnlοad=autoresize\\(this\\)", "xxxxx"); } String[] preg=new String[]{"<style.*?>[\\s\\S]*?<\\/style>","<noscript.*?>[\\s\\S]*?<\\/script>","<noscript.*?>[\\s\\S]*?<\\/noscript>","<select.*?>[\\s\\S]*?<\\/select>", "", "on[a-zA-Z]{3,16}\\s?=\\s?\"[\\s\\S]*?\""}; for(int i=0,size=preg.length;i<size;i++){ context="context.replaceAll(preg[i]," "");="" }="" if="" (content.indexof("onload")=""> -1) { content = content.replaceAll("onload", ""); } if (content.indexOf("xxxxx") > -1) { content = content.replaceAll("xxxxx", "οnlοad=autoresize(this)"); } if (content.indexOf("meta") > -1) { content = content.replaceAll("<meta.*?>", ""); } if (content.indexOf("a") > -1) { content = content.replaceAll("", ""); content = content.replaceAll("", ""); } if (content.indexOf("script") > -1) { content = content.replaceAll("<noscript.*?>", ""); content = content.replaceAll("</noscript>", ""); } if (content.indexOf("window") > -1) { content = content.replaceAll("window", ""); } if (content.indexOf("body") > -1) { content = content.replaceAll("<body.*?>", ""); } return content; } else { return ""; } } // 去除所有html标签 public static String removeAllTag(String content) { if (content.trim().equals("") && content == null) { return ""; } else { String str = content.replaceAll("\\&[a-zA-Z]{1,10};", "") .replaceAll("<[^>]*>", ""); str = str.replaceAll("[(/>)<]", ""); return str; } } // 获取超链接的地址 public static String getHtmlA(String content) { String str = ""; if (content == null && content.trim().length() < 0) { return ""; } Pattern pattern = Pattern.compile("href=\"(.+?)\"", Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(content); if (matcher.find()) { str = matcher.group(1); } return str; } // 截取社区签名的url public static String getHtmlUrl(String content) { String str = ""; if (content == null && content.trim().length() < 0) { return ""; } Pattern pattern = Pattern .compile("(http://|https://){1}[\\w\\.\\-/:]+", Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(content); StringBuffer buffer = new StringBuffer(); while (matcher.find()) { buffer.append(matcher.group()); // buffer.append(","); str = buffer.toString(); } return str; } // 去除社区特定签名的标签 public static String removeURL(String content) { String str = ""; if (content == null && content.trim().length() < 0) { return ""; } Pattern pattern = Pattern.compile("\\[url.*\\](.+?)\\[/url\\]"); Matcher matcher = pattern.matcher(content); while (matcher.find()) { str = matcher.group(1); } return str; } public static void main(String[] args) { // String ubb="[img]http://qimg1.51766.com/1208764354386/1217464310174/111264327/1217751798106.gif[/img][img]http://qimg1.51766.com/1208764354386/1217464310174/111264327/1217751798106.gif[/img][img]http://qimg1.51766.com/1208764354386/1217464310174/111264327/1217751798106.gif[/img][img]http://qimg1.51766.com/1208764354386/1217464310174/111264327/1217751798106.gif[/img][img]http://qimg1.51766.com/1208764354386/1217464310174/111264327/1217751798106.gif[/img][img]http://qimg1.51766.com/1208764354386/1217464310174/111264327/1217751798106.gif[/img][img]http://qimg1.51766.com/1208764354386/1217464310174/111264327/1217751798106.gif[/img][img]http://qimg1.51766.com/1208764354386/1217464310174/111264327/1217751798106.gif[/img][img]http://qimg1.51766.com/1208764354386/1217464310174/111264327/1217751798106.gif[/img][img]http://qimg1.51766.com/1208764354386/1217464310174/111264327/1217751798106.gif[/img][img]http://qimg1.51766.com/1208764354386/1217464310174/111264327/1217751798106.gif[/img][img]http://qimg1.51766.com/1208764354386/1217464310174/111264327/1217751798106.gif[/img][img]http://qimg1.51766.com/1208764354386/1217464310174/111264327/1217751798106.gif[/img][img]http://qimg1.51766.com/1208764354386/1217464310174/111264327/1217751798106.gif[/img][img]http://qimg1.51766.com/1208764354386/1217464310174/111264327/1217751798106.gif[/img][img]http://qimg1.51766.com/1208764354386/1217464310174/111264327/1217751798106.gif[/img][img]http://qimg1.51766.com/1208764354386/1217464310174/111264327/1217751798106.gif[/img][img]http://qimg1.51766.com/1208764354386/1217464310174/111264327/1217751798106.gif[/img]"; // System.out.println("ubb-----"+ubb); // ubb= escapeUBB2HTML(ubb); // System.out.println("html-----"+ubb); String ubb=""; String html="

 引用 zuzu08 的帖子

 

 引用 熊俊 的帖子

 1228817227138.gif\" 好看不知道好吃吗?



看起来可以,不知道怎么样


好吃就更好了 pic_bq13.gif\"
"; System.out.println("-html----"+html); System.out.println("--------------------------"); ubb=escapeHTML2UBB(html); System.out.println("ubb-----"+ubb); System.out.println("--------------------------"); ubb=escapeUBB2HTML(ubb); System.out.println("html-----"+ubb); } } 请高人指点。文中table遍历不够完美。。。。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值