/// <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;
}
含html标记的文本处理方法
最新推荐文章于 2022-03-14 20:40:15 发布