【问题标题】:How to HtmlEncode only text content in an HTML string?如何仅对 HTML 字符串中的文本内容进行 HtmlEncode?
【发布时间】:2019-04-12 00:50:34
【问题描述】:

我有一些 HTML 内容想要在我的网页中显示之前对其进行解析和编码。

诀窍是我只想编码文本内容,而不是 HTML 内容中明显的 HTML 标签。我怎样才能做到这一点?

例子:

提供

"Some text & links : <strong>bla blà blö</strong> and <a href="http://www.google.com">go there</a> for only 15 € < 20 €"

我想输出

"Some text &amp; links : <strong>bla bl&agrave; bl&ouml;</strong> and <a href="http://www.google.com">go there</a> for only 15 &euro; &lt; 20 &euro;"
or
"Some text &#38; links : <strong>bla bl&#224; bl&#246;</strong> and <a href="http://www.google.com">go there</a> for only 15 &#8364; &#60; 20 &#8364;"

【问题讨论】:

  • 你能提供一个例子来说明你到底想要完成什么吗? htmlencoding 的全部目的是对 HTML 标签进行编码...
  • 尝试使用像HTML Agility Pack这样的HTML解析器来进行实际的解析。
  • 我刚刚更新了一个例子。
  • 如果您将该字符串拆分为 HTMLString 和 ContentString,您可以对 ContentString 进行编码,然后将其与 HTMLString 连接起来。但这可能并不容易,除非您首先已经动态地构建了该字符串。 :)
  • 我想我不是第一个这样做的人。不知道有什么图书馆或什么可以帮助我做到这一点?

标签: c# html asp.net-mvc-3


【解决方案1】:

使用Html Agility Pack:

var html = 
  "Some text & links : <strong>bla blà blö</strong> and <a href=\"http://www.google.com\">go there</a> for only 15 € < 20 €";

// This
HtmlAgilityPack.HtmlEntity.Entitize(html);

// Outputs
Some text & links : <strong>bla bl&agrave; bl&ouml;</strong> and <a href="http://www.google.com">go there</a> for only 15 &euro; < 20 &euro;

刚刚对其进行了测试,它在您的示例中效果很好。

如果你想看看它是如何完成的,那就是public

【讨论】:

    【解决方案2】:

    我知道这是一个老话题,但我认为这个 sn-p 可能会做得很好。我也知道您不应该将 RegEx 用于 HTML 标记(因为它根本不处理 &lt;script&gt;&lt;style&gt;),但这种方法可能是您需要的,而不是获取整个 HTMLAgilityPack .... 我使用 SqlString 是因为我的 SQL Server 数据库使用了这种方法。可以很容易地切换到字符串。也很容易更改为 StringBuilder 以使其更优化。

    private static SqlString fnHTMLDecodeEncode(SqlString html, bool encode)
    {
      if (html.IsNull)
        return SqlString.Null;
    
      const RegexOptions REGOPT = RegexOptions.Singleline | RegexOptions.Compiled;
    
      string s = html.Value;
      var m = Regex.Matches(s, @"(<[!A-Za-z\/][^>]*>", RegexOptions.Singleline |   RegexOptions.Compiled);
      int proStart, proLen;
      if (m.Count == 0)
      {
        proStart = 0;
        proLen = s.Length;
      }
      else
      {
        proStart = m[m.Count - 1].Index + m[m.Count - 1].Length;
        proLen = s.Length - proStart;
      }
    
      for (int i = m.Count; i >= 0; i--)
      {
        if (i < m.Count)
        {
            proStart = (i == 0 ? 0 : m[i - 1].Index + m[i - 1].Length);
            proLen = m[i].Index - proStart;
        }
    
        if (proLen > 2)
        {
            var orig = s.Substring(proStart, proLen);
            var enc = (encode ? System.Net.WebUtility.HtmlEncode(orig) : System.Net.WebUtility.HtmlDecode(orig));
            if (orig.Length != enc.Length)
            {
                s = s.Remove(proStart, proLen).Insert(proStart, enc);
            }
    
            proLen = -1;
        }
    
      }
    
      return new SqlString(s);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-13
      • 2014-12-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多