含html标记的文本处理方法
来源:互联网 发布:p2p网络摄像机安卓软件 编辑:程序博客网 时间:2024/05/19 09:38
/// <summary> /// 去除给定内容中的html标记,只保留图片标记 /// </summary> /// <param name="strHtml">给定内容</param> protected string getContent(string strHtml) { string str = string.Empty; //str = Regex.Replace(strHtml, "<.+?>", "", RegexOptions.IgnoreCase);//一个去掉所有HTML标记 //str = Regex.Replace(strHtml, "/<(?!img).*?>/g", " ", RegexOptions.IgnoreCase);//这是保留Img和<br> //str = Regex.Replace(strHtml, "<(?!/?br|/?img)[^<>]*>", "", RegexOptions.IgnoreCase);//保留Img和<Br> str = Regex.Replace(strHtml, "<(?!/?img)[^<>]*>", "", RegexOptions.IgnoreCase);//保留Img //改Img为[图片] str = Regex.Replace(str, "<?img[^<>]*>", "[图片]", RegexOptions.IgnoreCase); //空格符 string strEmpty = " "; int intEmpy = strEmpty.Length; str = str.Trim(); //去除所有空格符 while (str.IndexOf(strEmpty) != -1) { str = str.Remove(str.IndexOf(strEmpty), intEmpy); } return str; }