使用正则表达式,其实也就是构造正则表达式复杂了些,也没什么难的。

附代码:

/// <summary>
/// 从文章内容中获取第一张图片,用作文章缩略图
/// </summary>
/// <param name="articleContent"></param>
/// <returns></returns>
public static string GetImageUrlFromArticle(string articleContent)
{
    Regex r = new Regex(@"<IMG[^>]+src=\s*(?:'(?<src>[^']+)'|""(?<src>[^""]+)""|(?<src>[^>\s]+))\s*[^>]*>", RegexOptions.IgnoreCase);
    MatchCollection mc = r.Matches(articleContent);
    if (mc.Count != 0)
    {
        return mc[0].Groups["src"].Value.ToLower();
    }
    else
    {
        return "";
    }
} 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-01-13
  • 2022-12-23
  • 2022-12-23
  • 2021-11-05
  • 2022-12-23
  • 2021-09-18
猜你喜欢
  • 2021-06-25
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-22
  • 2021-11-05
  • 2022-12-23
相关资源
相似解决方案