經常會遇到這樣的麻煩,客戶在復制Word中的內容到編輯器中,會發現有許多多余的代碼,又不能直接使用remove函數將所有的格式去掉,因為裡面有表格。所以想使用一個函數即可過濾不想要的內容,但又要保留下我想要的表格等內容。
<%
================================================
顯示解釋函數,返回根據參數允許顯示的格式字符串,具體調用方法可從後台管理獲得
輸入參數:
s_Content : 要轉換的數據字符串
sFilters: 要過濾掉的格式集,用逗號分隔多個
================================================
Function strFilter(s_Content, sFilters)
Dim a_Filter, i, s_Result, s_Filters
strFilter = s_Content
If IsNull(s_Content) Then Exit Function
If s_Content = Then Exit Function
s_Result = s_Content
s_Filters = sFilters
設置默認過濾
If sFilters = Then s_Filters = script,object
a_Filter = Split(s_Filters, ,)
For i = 0 To UBound(a_Filter)
s_Result = strFilterDecode(s_Result, a_Filter(i))
Next
strFilter = s_Result
End Function
%>
<Script Language=JavaScript RunAt=Server>
//===============================================
// 單個過濾
// 輸入參數:
// s_Content : 要轉換的數據字符串
// s_Filter : 要過濾掉的單個格式
//===============================================
function strFilterDecode(Html, filter){
switch(filter.toUpperCase()){
case SCRIPT: // 去除所有客戶端腳本Javascipt,vbscript,jscript,JS,vbs,event,...
html = strExecReg(</?script[>]*>, , Html);
Html = strExecReg((Javascript|JScript|vbscript|vbs):, $1:, Html);
html = strExecReg(on(mouse|exit|error|click|key), <I>on$1</I>, Html);
html = strExecReg(&#, <I>&#</I>, Html);
break;
case TABLE: // 去除表格<table><tr><td><th>
html = strExecReg(</?table[>]*>, , Html);
html = strExecReg(</?tr[>]*>, , Html);
html = strExecReg(</?th[>]*>, , Html);
html = strExecReg(</?td[>]*>, , Html);
break;
case CLASS: // 去除樣式類class=
html = strExecReg((<[>]) class=[ |>]*([>]*>), $1 $2, Html) ;
break;
case STYLE: // 去除樣式style=
html = strExecReg((<[>]) style=\[\]*\([>]*>), $1 $2, Html);
break;
case XML: // 去除XML<?XML>
html = strExecReg(<\\?XML[>]*>, , Html);
break;
case NAMESPACE: // 去除命名空間<o:p></o:p>
html = strExecReg(<\/?[a-z]:[>]*>, , Html);
break;
case FONT: // 去除字體<font></font>
html = strExecReg(</?font[>]*>, , Html);
break;
case MARQUEE: // 去除字幕<marquee></marquee>
html = strExecReg(</?marquee[>]*>, , Html);
break;
case OBJECT: // 去除對象<object><param><embed></object>
html = strExecReg(</?object[>]*>, , Html);
html = strExecReg(</?param[>]*>, , Html);
html = strExecReg(</?embed[>]*>, , Html);
break;
case IFRAME: // 去除框架<iframe></iframe>
html = strExecReg(</?iframe[>]*>, , Html);
break;
default:
}
return Html;
}
// ============================================
// 執行正則表達式替換
// ============================================
function strExecReg(re, rp, content) {
oReg = new RegExp(re, ig);
var r = content.replace(oReg, rp);
return r;
}
</script>
注意這裡的<Script Language=JavaScript RunAt=Server>說明這個JS只允許在服務器端運行,客戶端是看不到
Function strFilter(s_Content, sFilters)函數中的參數是自己想要過濾的內容,具體值在下面的JS函數中的case,大家根據自己的需要傳入參數。