我有一个asp.net网页,它有一个TinyMCE框。用户可以格式化文本并发送HTML以存储在数据库中。在服务器上,我想从文本中剥离HTML,这样我就可以只存储在全文索引列中的文本以供搜索。在客户端使用jQuery的text()函数剥离html是轻而易举的事,但我更愿意在服务器上这样做。
看我的回答。
alt text http://tinyurl.com/sillychimp
dsf9zpds1#
我下载了HtmlAgilityPack并创建了这个函数:
string StripHtml(string html){ // create whitespace between html elements, so that words do not run together html = html.Replace(">","> "); // parse html var doc = new HtmlAgilityPack.HtmlDocument(); doc.LoadHtml(html); // strip html decoded text from html string text = HttpUtility.HtmlDecode(doc.DocumentNode.InnerText); // replace all whitespace with a single space and remove leading and trailing whitespace return Regex.Replace(text, @"\s+", " ").Trim();}
string StripHtml(string html)
{
// create whitespace between html elements, so that words do not run together
html = html.Replace(">","> ");
// parse html
var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
// strip html decoded text from html
string text = HttpUtility.HtmlDecode(doc.DocumentNode.InnerText);
// replace all whitespace with a single space and remove leading and trailing whitespace
return Regex.Replace(text, @"\s+", " ").Trim();
}
字符串
imzjd6km2#
看看这个Strip HTML tags from a string using regular expressions
x3naxklr3#
查看以下示例:
TextReader tr = new StreamReader(@"Filepath");string str = tr.ReadToEnd(); str= Regex.Replace(str,"<(.|\n)*?>", string.Empty);
TextReader tr = new StreamReader(@"Filepath");
string str = tr.ReadToEnd();
str= Regex.Replace(str,"<(.|\n)*?>", string.Empty);
字符串但你需要有一个命名空间引用,即:
System.Text.RegularExpressions
型只有把这个逻辑为您的网站
von4xj4u4#
这是Jeff Atwood的Sanitize HTML method的RefactorMe代码链接
mpbci0fu5#
如果你只是为了索引而存储文本,那么你可能想做的不仅仅是删除HTML,比如忽略停止词和删除短于(比如)3个字符的单词。然而,我曾经写过一个简单的标签和剥离器是这样的:
public static string StripTags(string value) { if (value == null) return string.Empty; string pattern = @"&.{1,8};"; value = Regex.Replace(value, pattern, " "); pattern = @"<(.|\n)*?>"; return Regex.Replace(value, pattern, string.Empty); }
public static string StripTags(string value)
if (value == null)
return string.Empty;
string pattern = @"&.{1,8};";
value = Regex.Replace(value, pattern, " ");
pattern = @"<(.|\n)*?>";
return Regex.Replace(value, pattern, string.Empty);
字符串这是旧的,我相信它可以优化(也许使用编译的reg-ex?)。但它确实工作,可能会有所帮助。
ocebsuys6#
您可以:
hs1rzwqc7#
由于您可能在系统中有格式错误的HTML:BeautifulSoup或类似的可以使用。它是用Python编写的;我不确定它如何接口-使用.NET语言IronPython?
w7t8yxp58#
您可以使用HTQL COM,并使用查询来查询源:&tx;
sqserrrh9#
你可以用这个
string strwithouthtmltag; strwithouthtmltag = Regex.Replace(strWithHTMLTags, "<[^>]*>", string.Empty)
string strwithouthtmltag;
strwithouthtmltag = Regex.Replace(strWithHTMLTags, "<[^>]*>", string.Empty)
9条答案
按热度按时间dsf9zpds1#
我下载了HtmlAgilityPack并创建了这个函数:
字符串
imzjd6km2#
看看这个Strip HTML tags from a string using regular expressions
x3naxklr3#
查看以下示例:
字符串
但你需要有一个命名空间引用,即:
型
只有把这个逻辑为您的网站
von4xj4u4#
这是Jeff Atwood的Sanitize HTML method的RefactorMe代码链接
mpbci0fu5#
如果你只是为了索引而存储文本,那么你可能想做的不仅仅是删除HTML,比如忽略停止词和删除短于(比如)3个字符的单词。然而,我曾经写过一个简单的标签和剥离器是这样的:
字符串
这是旧的,我相信它可以优化(也许使用编译的reg-ex?)。但它确实工作,可能会有所帮助。
ocebsuys6#
您可以:
hs1rzwqc7#
由于您可能在系统中有格式错误的HTML:BeautifulSoup或类似的可以使用。
它是用Python编写的;我不确定它如何接口-使用.NET语言IronPython?
w7t8yxp58#
您可以使用HTQL COM,并使用查询来查询源:&tx;
sqserrrh9#
你可以用这个
字符串