導航:首頁 > 凈水問答 > html符號過濾類

html符號過濾類

發布時間:2023-06-01 13:53:54

『壹』 java 如何過濾html代碼,只保留中文或英文及基本常用符號

很容易,首先建立一個字元串數組,也就是你需要過濾掉的html標簽String[] filterArrays = new String[]{"<html>","</html>","<table>","</table>".....一系列內有關html標簽的東西}

當你得到一容個html代碼的字元串時你可以循環遍歷上面的數組,然後調用String自帶的方法replaceAll();
我給你簡單的示範一下啊
String str = "dfgdgdfgdgd";//需要過濾的帶有HTML標簽的代碼字元串
for(int i=0;i<filterArrays.length;i++){
if(str.indexOf(filterArrays[i])!=0){
str = str.replaceAll(filterArrays[i],"");//將html標簽替換成了空格
}
}

這樣就搞定了,主要是你需要在filterArrays中增加你需要過濾的字元串,當然還會有更好的辦法,可以不用增加這樣的數組,因為出現"<"必然會有">",或者"/>"這樣的標簽,但是這樣做可能會將一些無關的也過濾掉了,總之兩種方法都可以,第一種呢我都給你寫了例子!祝你成功啊

『貳』 字元串中如何過濾HTML標簽字元

下面是asp中的方法,你可以改造成.net的
Function FilterHTML(strToFilter)
Dim strTemp
strTemp = strToFilter
strTemp=replace(strTemp,"""","")
strTemp=replace(strTemp," ","")
strTemp=replace(strTemp," ","")
strTemp=replace(strTemp," ","")
strTemp=replace(strTemp,"&","")
Dim n,m '定義三個變數
n = inStr(strTemp,"<") '找到第一個"<"所在的位置
m = inStr(strTemp,">") '找到第一個">"所在的位置
Do while n > 0 and n < m '如果n>0則說明找到了一個"<",如果n<m則說明"<"在">"的左邊,則"<"和">"之間的字元串為HTML代碼,需要過濾掉
strTemp = Left(strTemp,n-1) & Mid(strTemp,m+1) '取"<"左邊的字元串和">"右邊的字元串並將他們連接在一起
n = inStr(strTemp,"<") '找到剩餘字元串中第一個"<"所在的位置
m = inStr(strTemp,">") '找到剩餘字元串中第一個">"所在的位置
Loop '循環
FilterHTML = strTemp
End Function

『叄』 正則表達式如何過濾HTML標簽中的屬性值

去掉html標簽: str.replace(/</?[a-zA-Z]+[^><]*>/g,"")
去掉標簽裡面的屬性: str.replace(/<([a-zA-Z]+)\s*[^><]*>/g,"<$1>")
我親自測試通過,操作語言專javascript 樓主還有問題的屬話Hi 我

『肆』 怎麼使用js過濾html標簽

你可以利用正則表達式來剔除這些標簽,也就是將所有的html類的標簽都替換為空即可:

//去除HTML標簽
str=str.replace(/</?[^>]*>/g,'');

『伍』 如何過濾HTML標簽,或者讀取數據時,去處HTML標簽

如果你把html標簽除掉了問題會更大。
如果你不需要所見即所得的編輯器,那麼可以直接使用textarea。在把用戶輸入的html標簽過濾掉就行了。

『陸』 怎麼過濾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;
}

『柒』 用正則表達式過濾HTML標簽

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* <p>
* Title: HTML相關的正則表達式工具類
* </p>
* <p>
* Description: 包括過濾HTML標記,轉換HTML標記,替換特定HTML標記
* </p>
* <p>
* Copyright: Copyright (c) 2006
* </p>
*
* @ hejian
* @version 1.0
* @createtime 2006-10-16
*/

public class HtmlRegexpUtil {
private final static String regxpForHtml = "<([^>]*)>"; // 過濾所有以<開頭以>結尾的標簽

private final static String regxpForImgTag = "<\\s*img\\s+([^>]*)\\s*>"; // 找出IMG標簽

private final static String regxpForImaTagSrcAttrib = "src=\"([^\"]+)\""; // 找出IMG標簽的SRC屬性

/**
*
*/
public HtmlRegexpUtil() {
// TODO Auto-generated constructor stub
}

/**
*
* 基本功能:替換標記以正常顯示
* <p>
*
* @param input
* @return String
*/
public String replaceTag(String input) {
if (!hasSpecialChars(input)) {
return input;
}
StringBuffer filtered = new StringBuffer(input.length());
char c;
for (int i = 0; i <= input.length() - 1; i++) {
c = input.charAt(i);
switch (c) {
case '<':
filtered.append("<");
break;
case '>':
filtered.append(">");
break;
case '"':
filtered.append(""");
break;
case '&':
filtered.append("&");
break;
default:
filtered.append(c);
}

}
return (filtered.toString());
}

/**
*
* 基本功能:判斷標記是否存在
* <p>
*
* @param input
* @return boolean
*/
public boolean hasSpecialChars(String input) {
boolean flag = false;
if ((input != null) && (input.length() > 0)) {
char c;
for (int i = 0; i <= input.length() - 1; i++) {
c = input.charAt(i);
switch (c) {
case '>':
flag = true;
break;
case '<':
flag = true;
break;
case '"':
flag = true;
break;
case '&':
flag = true;
break;
}
}
}
return flag;
}

/**
*
* 基本功能:過濾所有以"<"開頭以">"結尾的標簽
* <p>
*
* @param str
* @return String
*/
public static String filterHtml(String str) {
Pattern pattern = Pattern.compile(regxpForHtml);
Matcher matcher = pattern.matcher(str);
StringBuffer sb = new StringBuffer();
boolean result1 = matcher.find();
while (result1) {
matcher.appendReplacement(sb, "");
result1 = matcher.find();
}
matcher.appendTail(sb);
return sb.toString();
}

/**
*
* 基本功能:過濾指定標簽
* <p>
*
* @param str
* @param tag
* 指定標簽
* @return String
*/
public static String fiterHtmlTag(String str, String tag) {
String regxp = "<\\s*" + tag + "\\s+([^>]*)\\s*>";
Pattern pattern = Pattern.compile(regxp);
Matcher matcher = pattern.matcher(str);
StringBuffer sb = new StringBuffer();
boolean result1 = matcher.find();
while (result1) {
matcher.appendReplacement(sb, "");
result1 = matcher.find();
}
matcher.appendTail(sb);
return sb.toString();
}

/**
*
* 基本功能:替換指定的標簽
* <p>
*
* @param str
* @param beforeTag
* 要替換的標簽
* @param tagAttrib
* 要替換的標簽屬性值
* @param startTag
* 新標簽開始標記
* @param endTag
* 新標簽結束標記
* @return String
* @如:替換img標簽的src屬性值為[img]屬性值[/img]
*/
public static String replaceHtmlTag(String str, String beforeTag,
String tagAttrib, String startTag, String endTag) {
String regxpForTag = "<\\s*" + beforeTag + "\\s+([^>]*)\\s*>";
String regxpForTagAttrib = tagAttrib + "=\"([^\"]+)\"";
Pattern patternForTag = Pattern.compile(regxpForTag);
Pattern patternForAttrib = Pattern.compile(regxpForTagAttrib);
Matcher matcherForTag = patternForTag.matcher(str);
StringBuffer sb = new StringBuffer();
boolean result = matcherForTag.find();
while (result) {
StringBuffer sbreplace = new StringBuffer();
Matcher matcherForAttrib = patternForAttrib.matcher(matcherForTag
.group(1));
if (matcherForAttrib.find()) {
matcherForAttrib.appendReplacement(sbreplace, startTag
+ matcherForAttrib.group(1) + endTag);
}
matcherForTag.appendReplacement(sb, sbreplace.toString());
result = matcherForTag.find();
}
matcherForTag.appendTail(sb);
return sb.toString();
}
}

『捌』 jQuery 過濾html標簽屬性的特殊字元

您好,如果在表單中需要提交一字元串,其中包含,< > " &字元時,當我們把這字元串顯示到jsp頁面時,會和html標簽產生沖突,導致web頁面的某些部分消失或者格式不正確。為了解決以上問題,需要在顯示之前,對字元串進行代碼過濾。
把字元串中的 < 替換為 &It;
> 替換為 >
" 替換為 "
& 替換為 &
這里給出一個靜態的過濾代碼,供大家參考:
public class StringUtils {
/**
* This method takes a string which may contain HTML tags (ie, <b>,
* <table>, etc) and converts the '<'' and '>' characters to their HTML escape sequences.
* @param input the text to be converted.
* @return the input string with the characters '<' and '>' replaced with their HTML escape sequences.
*/
public static final 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;
}
//Use a StringBuffer in lieu of String concatenation -- it is
//much more efficient this way.
StringBuffer buf = new StringBuffer(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 if(ch == '&'){
buf.append("&");
}
else {
buf.append(ch);
}
}
return buf.toString();
}
}
此時,只需在jsp中對字元串調用此方法(StringUtils.escapeHTMLTags(str))即可。

『玖』 JS正則過濾指定的HTML標簽

1,得到網頁上的鏈接源地址:

string
matchString =
@"<a[^>]+href=\s*(?:'(?<href>[^']+)'|""(?<href>[^""]+)""|(?<href>[^>\s]+))\s*[^>]*>";
2,得到網頁的標題:
string matchString = @"<title>(?<title>.*)</title>";
3,去掉網頁中的所有的html標記:
string temp = Regex.Replace(html, "<[^>]*>", ""); //html是一個要去除html標記的文檔

4, string matchString = @"<title>([\S\s\t]*?)</title>";
5,js去掉所有html標記的函數:
function delHtmlTag(str)
{
return str.replace(/<[^>]+>/g,"");//去掉所有的html標記
}

『拾』 C# 堆文本編輯器裡面的內容html標簽進行過濾,除了a標簽以外全部過濾

用正則表抄達式來襲做
引用 using System.Text.RegularExpressions;

Match result;
result = Regex.Match(文本, @"(?<value><a>.+?<\/a>)");
string getstring = string.Empty;
while (result.Success)
{
getstring += result.Groups["value"].Value;
result = result.NextMatch();
}
getstring就是內容了

閱讀全文

與html符號過濾類相關的資料

熱點內容
男孩裸體電影 瀏覽:863
國產老電影《主流 瀏覽:896
在功放前面加個解碼器對音質有提升嗎 瀏覽:643
怎麼樣去除電水壺里的水垢 瀏覽:958
污水mbr平板膜怎麼維護 瀏覽:541
污水處理格柵的長寬是多少 瀏覽:269
氮氣進超純水設備怎麼接管道 瀏覽:954
韓國 小電影 瀏覽:846
日韓中文字幕在線免費 瀏覽:853
武漢新明纖維樹脂製品有限公 瀏覽:228
狄仁傑殺鳳凰完整版 瀏覽:94
戴森v7濾芯如何清潔 瀏覽:377
韓國倫理中文版 瀏覽:181
環氧樹脂接著劑特點 瀏覽:505
進口高揚程污水提升泵 瀏覽:380
污水管線施工最少多少米一段 瀏覽:371
愛情電影網怎麼進不去了 瀏覽:901
課中壞事金發女演員叫什麼 瀏覽:548
籠中鳥奧利弗在哪能看 瀏覽:985
哪裡有免費的網站可以看 瀏覽:317