///
/// 去除 HTML 標籤,可自訂合法標籤加以保留
///
/// 來源字串
/// 合法標籤集
///
public static string StripTags(string src, string[] reservedTagPool)
{
return Regex.Replace(
src,
String.Format("<(?!{0}).*?>", string.Join("|", reservedTagPool)),
String.Empty);
}
using System;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string html = "<div><p>OOXXOOXXOOXXOOXXOOXXOOXX!" + "<img src=\"http://www.OX.com.tw/Images/LoGO.gif\" alt=\"Logo\" /></p></div>";
string[] reservedTagPool = { "img", "br" };
TextBox1.Text = html;
TextBox2.Text = HtmlRemoval.StripTags(html, reservedTagPool);
Literal1.Text = HtmlRemoval.StripTags(html, reservedTagPool);
}
}
轉自
http://www.dotblogs.com.tw/hunterpo/archive/2010/11/26/19732.aspx
沒有留言:
張貼留言