當前位置:首頁 » 凈水方式 » 過濾網頁代碼

過濾網頁代碼

發布時間: 2021-02-24 06:06:58

過濾除表格以外的所有html代碼,如何實現

public static string NoHTML(string Htmlstring)
{
//刪除腳本
Htmlstring=Htmlstring.Replace("'", "'");
Htmlstring = Regex.Replace(Htmlstring, @" <script[^>]*?>.*? </script>", "",RegexOptions.IgnoreCase);
//刪除HTML 下面一行刪除,涉及到表格裡面的table tr td
//Htmlstring = Regex.Replace(Htmlstring, @" <(.[^(table|tr|td|>)]*)>", "",RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "",RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @" <!--.*", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"",RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&",RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", " <",RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">",RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ",RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1",RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2",RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3",RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(|#169);", "\xa9",RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "",RegexOptions.IgnoreCase);
Htmlstring.Replace(" <", "");
Htmlstring.Replace(">", "");
Htmlstring.Replace("\r\n", "");
Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
return Htmlstring;
}

上面已經修改,只用取消Htmlstring = Regex.Replace(Htmlstring, @" <(.[^(table|tr|td|>)]*)>", "",RegexOptions.IgnoreCase); 這一個就行了。

㈡ 怎麼過濾html標簽

過濾html標簽代碼如下:
public string checkStr(string html)
{
System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"<script[\s\S]+</script *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@" href *= *[\s\S]*script *:", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex3 = new System.Text.RegularExpressions.Regex(@" on[\s\S]*=", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex4 = new System.Text.RegularExpressions.Regex(@"<iframe[\s\S]+</iframe *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex5 = new System.Text.RegularExpressions.Regex(@"<frameset[\s\S]+</frameset *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex6 = new System.Text.RegularExpressions.Regex(@"\<img[^\>]+\>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex7 = new System.Text.RegularExpressions.Regex(@"</p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex8 = new System.Text.RegularExpressions.Regex(@"<p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex9 = new System.Text.RegularExpressions.Regex(@"<[^>]*>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
html = regex1.Replace(html, ""); //過濾<script></script>標記
html = regex2.Replace(html, ""); //過濾href=javascript: (<A>) 屬性
html = regex3.Replace(html, " _disibledevent="); //過濾其它控制項的on...事件
html = regex4.Replace(html, ""); //過濾iframe
html = regex5.Replace(html, ""); //過濾frameset
html = regex6.Replace(html, ""); //過濾frameset
html = regex7.Replace(html, ""); //過濾frameset
html = regex8.Replace(html, ""); //過濾frameset
html = regex9.Replace(html, "");
html = html.Replace(" ", "");
html = html.Replace("</strong>", "");
html = html.Replace("<strong>", "");
return html;
}

㈢ 寫一段js代碼過濾一段內容的里html代碼

缺少你的代碼可參照,看不到問題

㈣ 過濾html代碼的辦法.

郵件發給你

㈤ java中如何過濾html的代碼

||把需要寫入資料庫的字元通過下面的方法過濾然後再寫入回 public static String converthtml(String input) { if (input == null || input.length() == 0) { return input; } StringBuffer buf = new StringBuffer(input.length() + 6); 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 if (ch == ' ') { buf.append(""); } else { buf.append(ch); } } return buf.toString(); }

希望采答納

㈥ 怎樣用js方法過濾html等代碼,如@

關鍵點:

  1. 正則表達式,把要替換的內容用正則表達式表達出來,如字元串、數字、字母版中文、標點符號等。權

  2. replace() 方法,用於在字元串中用一些字元替換另一些字元,或替換一個與正則表達式匹配的子串。



上代碼:

<html>
<head>
<title>無標題文檔</title>
<metacharset="UTF-8">
</head>
<body>
<divid="main">文章內容@文章內容,文章內容@文章內容</div>
</body>

<script>
//獲取標簽中文本
vardoj_str=document.getElementById('main').innerText;
//要替換的字元串,最後的g表示全局匹配,例如又多個@
varreg_str=/@/g;
//替換為空
varnew_str=doj_str.replace(reg_str,'');
//輸出新字元串
document.write(new_str);
</script>

</html>

㈦ 如何把HTML代碼過濾一下

這是一個刪除發貼字元串中HTML代碼的函數。具體解釋如下:

Function KillHTMLLabel(str) '函數開始

Dim n,m,str2 '定義三個變數
n = inStr(str,"<") '找到第一個"<"所在的位置
m = inStr(str,">") '找到第一個">"所在的位置
str2 = str '把str的值賦給str2
Do while n > 0 and n < m '如果n>0則說明找到了一個"<",如果n<m則說明"<"在">"的左邊,則"<"和">"之間的字元串為HTML代碼,需要過濾掉
str2 = Left(str2,n-1) & Mid(str2,m+1) '取"<"左邊的字元串和">"右邊的字元串並將他們連接在一起
n = inStr(str2,"<") '找到剩餘字元串中第一個"<"所在的位置
m = inStr(str2,">") '找到剩餘字元串中第一個">"所在的位置
Loop '循環
KillHTMLLabel = str2 '將過濾好的字元串返回。

End Function '結束。
這只是個簡單的函數。
對於這樣的字元串他是無法過濾的:><html>,這個字元串因為在運行第一遍循環時不符合條件,所以程序就會跳到loop後面,但是這個字元串中的html代碼卻不能過濾掉。
最好的辦法是用正則表達式來過濾

㈧ 求助 SQL語句 過濾HTML代碼

<%Function htmlDecode(strHTML)
Dim objRegExp, Match, Matches
Set objRegExp = New Regexp
objRegExp.IgnoreCase = True
objRegExp.Global = True
'取閉合的<>
objRegExp.Pattern = "<.+?>"
'進行匹配
Set Matches = objRegExp.Execute(strHTML)
' 遍歷匹配集合,並替換掉匹版配的項目
For Each Match in Matches
strHtml=Replace(strHTML,Match.Value,"")
Next
htmlDecode=strHTML
Set objRegExp = Nothing
End Function
%>

調用函權數

㈨ 怎樣用js方法過濾html等代碼

^<input type="text" id="theOne" value="">
<input type="button" onclick="NoHtml()" value="過濾html標簽">
<script>
function NoHtml(){
var t=document.getElementById("theOne").value;
t=t.replace(/({|})/g,''); //過濾{}
t=t.replace(/</g,'<'); //置換符號<
t=t.replace(/>/g,'>'); //置換符號>
// t=t.replace(/<\/?[^>]*>/g,''); //*<\/?[^>]*>可以匹配<script></style></body>等,版並置空權。而不是替換<和>兩個符號
document.getElementById("theOne").value=t;
}
</script>

㈩ 輸出數據時,如何過濾html代碼

這個方法在網上很流行的,你看看
<%
Function RemoveHTML(strHTML)
Dim objRegExp, Match, Matches
Set objRegExp = New Regexp

objRegExp.IgnoreCase = True
objRegExp.Global = True
'取閉合的<>
objRegExp.Pattern = "<.+?>"
'進行匹配
Set Matches = objRegExp.Execute(strHTML)

' 遍歷匹配集合,內並替換掉匹配的容項目
For Each Match in Matches
strHtml=Replace(strHTML,Match.Value,"")
Next
RemoveHTML=strHTML
Set objRegExp = Nothing
End Function
%>

熱點內容
丁度巴拉斯情人電影推薦 發布:2024-08-19 09:13:07 瀏覽:886
類似深水的露點電影 發布:2024-08-19 09:10:12 瀏覽:80
《消失的眼角膜》2電影 發布:2024-08-19 08:34:43 瀏覽:878
私人影院什麼電影好看 發布:2024-08-19 08:33:32 瀏覽:593
干 B 發布:2024-08-19 08:30:21 瀏覽:910
夜晚看片網站 發布:2024-08-19 08:20:59 瀏覽:440
台灣男同電影《越界》 發布:2024-08-19 08:04:35 瀏覽:290
看電影選座位追女孩 發布:2024-08-19 07:54:42 瀏覽:975
日本a級愛情 發布:2024-08-19 07:30:38 瀏覽:832
生活中的瑪麗類似電影 發布:2024-08-19 07:26:46 瀏覽:239