如何使用asp.net从字符串中删除HTML标记(粗体,强,字体名称等) [英] How to remove HTML tags from string using asp.net (Bold,strong,font names etc etc)

查看:75
本文介绍了如何使用asp.net从字符串中删除HTML标记(粗体,强,字体名称等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用CKEditor输入详细信息(详细信息包含html元素)。之后,细节将绑定到网格中。在这里我想在工具提示中显示没有html标签的细节。



  protected   void  grdStepMain_RowDataBound( object  sender,GridViewRowEventArgs e)
{
< span class =code-keyword> int i = 0 ;
if (e.Row.RowType == DataControlRowType.DataRow)
{
foreach (TableCell cell in e.Row.Cells)
{
i ++;
string description = cell.Text;
if (cell.Text.Length > 8 &&(i == 2 ))
cell.Text = cell.Text.Substring( 0 8 )+ ....;
string newdescription = Regex.Replace(description, @ < [^>] +> |& nbsp; )修剪();
cell.ToolTip = newdescription;
}

}
}





但它不起作用请帮助我。示例:



  string  str = & lt; h1& gt;& lt; span style =& quot; font-family:courier  new  courier ,monospace& quot;& gt;& lt; strong& gt; 457457& lt; / strong& gt; 544444444444457457457& lt; / span& gt;; 

result in tooltip 457457544444444444457457457









请帮我



问候

jithesh A

解决方案

In您的评论,您说HTML标签实际上是由HTML实体组成的。在这种情况下,从< > 匹配的正则表达式将不起作用;你需要一个匹配从& lt; & gt;
$ b $的正则表达式b

  string  newdescription = Regex.Replace(description, @ & lt;。+?& gt; |& nbsp; )修剪(); 


I am using CKEditor to enter details (The details contains html elements). after that the details will be bind into a grid. In this I want to show the details without html tags in tooltip.

protected void grdStepMain_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        int i = 0;
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            foreach (TableCell cell in e.Row.Cells)
            {
                i++;
                string description = cell.Text;
                if (cell.Text.Length > 8 && (i == 2))
                    cell.Text = cell.Text.Substring(0, 8) + "....";
              string newdescription = Regex.Replace(description, @"<[^>]+>|&nbsp;", "").Trim();
              cell.ToolTip = newdescription;
            }

        }
    }



But its not working please help me. Example:

string str= " &lt;h1&gt;&lt;span style=&quot;font-family:courier new,courier,monospace&quot;&gt;&lt;strong&gt;457457&lt;/strong&gt; 544444444444457457457&lt;/span&gt;";

The result in tooltip should be 457457544444444444457457457





Please hep me

Regards
jithesh A

解决方案

In your comment, you said that the HTML tags are actually made up with HTML Entities. In that case, a regex that matches from < to > won't work; you need a regex that matches from &lt; to &gt;:

string newdescription = Regex.Replace(description, @"&lt;.+?&gt;|&nbsp;", "").Trim();


这篇关于如何使用asp.net从字符串中删除HTML标记(粗体,强,字体名称等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆