1

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

/// <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);
    }
}
4

1 回答 1

1

您刚刚删除 HTML 标记的代码,它不会转换&amp;'s 和&nbsp;'s。

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

于 2012-08-24T15:56:37.483 回答