background image
namespace shangyun2
{
class cutHtml
{
/// <summary>
/// 截取带 html 标签的字符串
/// </summary>
/// <param name="html">源字符串</param>
/// <param name="count">要截取的字符数</param>
/// <param name="script">是否保留 JS 脚本</param>
/// <param name="tags">要去掉的 html 标签</param>
/// <returns></returns>
public string CutStringWithHtmlTag(string html, int count, bool scri
pt, params string[] tags)
{
//
if (string.IsNullOrEmpty(html) || count <= 0)
{
throw new ArgumentException();
}
//
if (script == false)
{
Regex regScript = new Regex("<script.*?</script>");
html = regScript.Replace(html, "");
}
//
Regex regImg = new Regex("(?<IMG><img.*?/?)>");
MatchCollection matchCol = regImg.Matches(html);
if (matchCol.Count > 0)
{
foreach (Match match in matchCol)
{
html = regImg.Replace(html, match.Groups["IMG"].Value + "
www.mntuku.cn/>");
}
}
//待优化 读取出来的字符串中的换号和回车\r\n,这里暂未处理