2012-08-24 80 views
1

我正在讨论网站上工作(http://www.wrangle.in/)。我添加了HTML格式的主题描述的新功能。 HTML格式的描述保存在数据库中。加载主题时,说明以HTML格式显示。但是,即使在我使用以下类从字符串中删除HTML标记之后,元描述仍会显示HTML标记。但这个班没有工作。我从网上的某个地方下载了它。它不是删除&,& amp;等等。 即使它不删除所有的标签。请告诉我如何让我的HTML描述在META中作为文本描述可见?将HTML描述转换为字符串以设置为元描述

/// <summary> 
/// Methods to remove HTML from strings. 
/// </summary> 
public static class HtmlRemoval 
{ 
    /// <summary> 
    /// Remove HTML from string with Regex. 
    /// </summary> 
    public static string StripTagsRegex(string source) 
    { 
     return Regex.Replace(source, "<.*?>", string.Empty); 
    } 

    /// <summary> 
    /// Compiled regular expression for performance. 
    /// </summary> 
    static Regex _htmlRegex = new Regex("<.*?>", RegexOptions.Compiled); 

    /// <summary> 
    /// Remove HTML from string with compiled Regex. 
    /// </summary> 
    public static string StripTagsRegexCompiled(string source) 
    { 
     return _htmlRegex.Replace(source, string.Empty); 
    } 

    /// <summary> 
    /// Remove HTML tags from string using char array. 
    /// </summary> 
    public static string StripTagsCharArray(string source) 
    { 
     char[] array = new char[source.Length]; 
     int arrayIndex = 0; 
     bool inside = false; 

     for (int i = 0; i < source.Length; i++) 
     { 
      char let = source[i]; 
      if (let == '<') 
      { 
       inside = true; 
       continue; 
      } 
      if (let == '>') 
      { 
       inside = false; 
       continue; 
      } 
      if (!inside) 
      { 
       array[arrayIndex] = let; 
       arrayIndex++; 
      } 
     } 
     return new string(array, 0, arrayIndex); 
    } 
} 

回答

1

你的代码只是删除HTML标签,也不会转换&amp;的和&nbsp;的。

使用HttpUtility.HtmlDecode会将它们转换为可读的字符。

+0

其实它在HttpUtility命名空间我觉得。但它仍然不工作,删除标签,但不是其他  like words –

+0

嗯,解码字符串应该已经转换了字符,你使用MVC构建的任何机会? –