Lấy toàn bộ thẻ src và <img /> của chuỗi html truyền vào trong asp.net

Thảo luận trong 'Lập trình web Asp.net' bắt đầu bởi admin, 7/3/18.

  1. admin
    Tham gia ngày:
    22/5/13
    Bài viết:
    4,878
    Đã được thích:
    1,193
    Điểm thành tích:
    113
    Giới tính:
    Nam
    Việc lấy các thuộc tính, thẻ img trong asp.net thường xuyên cần dùng khi bạn lập trình web asp.net. Nếu tự code tay để lấy các thuộc tính src, thẻ <img /> trong 1 chuỗi html truyền vào rất lâu và khó khăn. Để giải quyết vấn đề này ta sử dụng các hàm có sẵn trong thư viên asp.net là Regex để lấy các thẻ src, img này một cách nhanh chóng.

    1. Lấy toàn bộ thẻ <img /> trong chuỗi html truyền vào asp.net.
    • Lấy toàn bộ thẻ <img /> truyền vào bao gồm toàn bộ thuộc tình src, alt, style,.. có trong thẻ <img />
    Demo:
    Mã:
    string str = "Thẻ hình ảnh <img style='width: 48%;' alt='Link Nofollow' src='https://itseovn.com/images/Link-Nofollow-Dofollow-la-gi-khac-biet-giua-Nofollow-va-Dofollow.jpg' />, <span>lấy thẻ src, thẻ hình ảnh toàn bộ trong asp.net</span>, <img style='width: 48%;' alt='Link Nofollow' src='https://itseovn.com/attachments/google-webmaster-tools-logo-jpg.26/' />";
    Kêt quả lấy :
    Mã:
    <img style='width: 48%;' alt='Link Nofollow' src='https://itseovn.com/images/Link-Nofollow-Dofollow-la-gi-khac-biet-giua-Nofollow-va-Dofollow.jpg' />
    Và
    <img style='width: 48%;' alt='Link Nofollow' src='https://itseovn.com/attachments/google-webmaster-tools-logo-jpg.26/' />
    
    Code lấy toàn bộ thẻ img trong html asp.net
    Mã:
    using System.Text.RegularExpressions;
    using System.Collections.Generic;
    
    private List<string> GetImagesInHTMLString(string htmlString)
    {
        List<string> images = new List<string>();
        string pattern = @"<(img)\b[^>]*>";
        Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
        MatchCollection matches = rgx.Matches(htmlString);
        for (int i = 0, l = matches.Count; i < l; i++)
        {
            if (matches[i].Value.Trim() != "")
            {
                images.Add(matches[i].Value);
            }
        }
        return images;
    }
    2. Lấy toàn bộ thẻ thuộc tính SRC trong thẻ <img /> của chuỗi html truyền vào asp.net
    • Lấy toàn bộ thuộc tính src trong thẻ <img /> truyền vào bao gồm toàn bộ thuộc tình src, alt, style,.. có trong thẻ <img />
    Demo:
    Mã:
    string str = "Thẻ hình ảnh <img style='width: 48%;' alt='Link Nofollow' src='https://itseovn.com/images/Link-Nofollow-Dofollow-la-gi-khac-biet-giua-Nofollow-va-Dofollow.jpg' />, <span>lấy thẻ src, thẻ hình ảnh toàn bộ trong asp.net</span>, <img style='width: 48%;' alt='Link Nofollow' src='https://itseovn.com/attachments/google-webmaster-tools-logo-jpg.26/' />";
    Kêt quả lấy :
    Code lấy toàn bộ thẻ src trong html asp.net
    Mã:
    private List<string> GetSrcImagesInHTMLString(string htmlString)
    {
        List<string> images = new List<string>();
        var imgreg = Regex.Matches(htmlString, @"(?<=<img\s+[^>]*?src=(?<q>['""]))(?<url>.+?)(?=\k<q>)");
        var list = new HashSet<String>(); //HashSet trùng nhau thì không lấy
        foreach (var img in imgreg)
        {
            if (img.ToString() != "")
            {
                images.Add(img.ToString());
            }
        }
        return images;
    }
    
    Từ khóa tiếng anh: Regex get the src of img attribute in html asp.net, Extract image tags from HTML in C#, Regular Expression to manipulare "src " attribute in HTML <img, Get all the <img src=""> tag in html, How can i get the img element and src attribute from the html text, Using a Regular Expression to Match HTML get src img in asp.net, regex - Regular Expression to get the SRC of images in C#, Regular Expression to manipulare "src " attribute in HTML, How to get the source of Image using c# regular Expression
     
    Cảm ơn đã xem bài:

    Lấy toàn bộ thẻ src và <img /> của chuỗi html truyền vào trong asp.net

  2. vjetdung96
    Tham gia ngày:
    22/1/18
    Bài viết:
    126
    Đã được thích:
    5
    Điểm thành tích:
    18
    Giới tính:
    Nam
    asp.net học làm gì nữa. Lỗi thời rồi
     


Chủ để tương tự : Lấy toàn
Diễn đàn Tiêu đề Date
Lập trình web Asp.net Lấy chiều cao và rộng của HttpPostedFileBase, get width Height HttpPostedFileBase 25/11/20
Lập trình web Asp.net Lấy Id Event OnClick Button trong Page_Load asp.net c# 24/10/18
Lập trình web Asp.net Get item value HashSet, lấy dữ liệu item HashSet<> c# asp.net MVC 8/9/18
Lập trình web Asp.net Lấy IP máy Client người dùng trong asp.net (C#) như thế nào 27/3/18
Lập trình web Asp.net Lấy lại đường dẫn URL truy cập trước đó trong asp.net 30/11/17