导航:首页 > 净水问答 > 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符号过滤类相关的资料

热点内容
怎么出去加湿器的水垢 浏览:662
美国电影x等级 浏览:700
污水井内用什么防毒 浏览:85
硫氰酸钠过反渗透膜 浏览:425
脱硫废水的水质特性 浏览:666
为什么用超滤水管道才接ts之高 浏览:778
怎么用消毒液清理饮水机 浏览:350
请解释有机蒸馏中加入沸石防止暴沸的原理 浏览:918
食品污水处理站污泥量计算 浏览:8
关于保姆的美国电影 浏览:22
焦作碧丽净水分公司在哪里 浏览:858
泰兴市滨江污水处理厂污泥事件6 浏览:792
丰田sw4空调滤芯位置在哪里 浏览:272
捉鬼合家欢国语电影在线观看 浏览:395
买的净水器换过滤器怎么办 浏览:494
生活污水tp超标是什么意思 浏览:210
沁园净水器水龙头隐藏螺丝在哪里 浏览:880
西门子洗衣机iq300如何除垢 浏览:837
高温水箱电子除垢仪 浏览:451
东阳之睛树脂镜片 浏览:465