BomKhung.Com Đã Quay Trở Lại
Hoạt Động Dưới Tên Miền Hung.Pro.VN
00 Days
00 Hours
00 Minutes
00 Seconds
Hiện website đang cập nhật nội dung bài viết, nếu có lỗi gì mọi người có thể thông báo cho mình Tại đây!

[VB.NET] Instructions for converting HTML code to BB code

Bài viết hôm nay, mình xin hướng dẫn các bạn cách chuyển đổi code từ code HTML to BBcode. Trong bài viết trước, mình đã có làm một bài hướng dẫn về cách đăng tin tự động lên diễn đàn Xenforo. Để đăng lên được các diễn đàn Forum, các bạn cần phải convert code từ HTML sang BBcode, thì post nó mới hiểu.
[VB.NET] Instructions for converting HTML code to BB code
Bảng tag element của HTML BBcode.
Dưới đây, là hàm convert HTML to BBcode, các bạn chỉ cần sử dụng Regular Expression đề xử lý.
Các bạn có thể tìm hiểu bài Regular trong lập trình Csharp để tham khảo.

FULL CODE

Public Function FormatHtmlIntoBBCode(desc As String) As String
        desc = System.Text.RegularExpressions.Regex.Replace(desc, "<br(.*?)>", "[br]", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        desc = System.Text.RegularExpressions.Regex.Replace(desc, "<br(.*?)>", "[br]", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        desc = System.Text.RegularExpressions.Regex.Replace(desc, "<ul[^>]*>", "[ulist]", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        desc = System.Text.RegularExpressions.Regex.Replace(desc, "", "[/ulist]", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        desc = System.Text.RegularExpressions.Regex.Replace(desc, "<ol[^>]*>", "[olist]", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        desc = System.Text.RegularExpressions.Regex.Replace(desc, "", "[/olist]", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        desc = System.Text.RegularExpressions.Regex.Replace(desc, "<li>", "[*]", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        desc = System.Text.RegularExpressions.Regex.Replace(desc, "</li>", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        desc = System.Text.RegularExpressions.Regex.Replace(desc, "<b>", "[b]", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        desc = System.Text.RegularExpressions.Regex.Replace(desc, "</b>", "[/b]", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        desc = System.Text.RegularExpressions.Regex.Replace(desc, "<strong>", "[strong]", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        desc = System.Text.RegularExpressions.Regex.Replace(desc, "</strong>", "[/strong]", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        desc = System.Text.RegularExpressions.Regex.Replace(desc, "<u>", "[u]", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        desc = System.Text.RegularExpressions.Regex.Replace(desc, "</u>", "[/u]", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        desc = System.Text.RegularExpressions.Regex.Replace(desc, "<i>", "[i]", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        desc = System.Text.RegularExpressions.Regex.Replace(desc, "</i>", "[/i]", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        desc = System.Text.RegularExpressions.Regex.Replace(desc, "<em>", "[em]", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        desc = System.Text.RegularExpressions.Regex.Replace(desc, "</em>", "[/em]", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        desc = System.Text.RegularExpressions.Regex.Replace(desc, "<sup>", "[sup]", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        desc = System.Text.RegularExpressions.Regex.Replace(desc, "</sup>", "[/sup]", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        desc = System.Text.RegularExpressions.Regex.Replace(desc, "<sub>", "[sub]", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        desc = System.Text.RegularExpressions.Regex.Replace(desc, "</sub>", "[/sub]", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        desc = System.Text.RegularExpressions.Regex.Replace(desc, "<hr[^>]*>", "[hr]", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        desc = System.Text.RegularExpressions.Regex.Replace(desc, "<strike>", "[strike]", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        desc = System.Text.RegularExpressions.Regex.Replace(desc, "</strike>", "[/strike]", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        desc = System.Text.RegularExpressions.Regex.Replace(desc, "<h1>", "[h1]", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        desc = System.Text.RegularExpressions.Regex.Replace(desc, "</h1>", "[/h1]", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        desc = System.Text.RegularExpressions.Regex.Replace(desc, "<h2>", "[h2]", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        desc = System.Text.RegularExpressions.Regex.Replace(desc, "</h2>", "[/h2]", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        desc = System.Text.RegularExpressions.Regex.Replace(desc, "<h3>", "[h3]", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        desc = System.Text.RegularExpressions.Regex.Replace(desc, "</h3>", "[/h3]", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
 
        'desc = System.Text.RegularExpressions.Regex.Replace(desc, "[br]", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        Dim match As System.Text.RegularExpressions.MatchCollection = System.Text.RegularExpressions.Regex.Matches(desc, "<img[ss]*?src=""([ss]*?)""[ss]*?>", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        If match.Count > 0 Then
            desc = System.Text.RegularExpressions.Regex.Replace(desc, match(0).ToString(), "[img]" + match(0).Groups(1).Value + "[/img]", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        End If
        desc = System.Text.RegularExpressions.Regex.Replace(desc, "<a href="" ",="" "[url=", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        desc = System.Text.RegularExpressions.Regex.Replace(desc, " <="" a="">", "[/url]", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        'desc = System.Text.RegularExpressions.Regex.Replace(desc, "<span style="" color:",="" "[color=", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        'desc = System.Text.RegularExpressions.Regex.Replace(desc, " <="" span="">", "[/color]", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        desc = System.Text.RegularExpressions.Regex.Replace(desc, """>", "]", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        desc = desc.Replace("[br]", "")
        desc = desc.Replace("<p>", "")
        desc = desc.Replace("</p>", "")
        Return desc
End Function

Chúc các bạn thành công với thủ thuật trên. 

Đăng nhận xét

Đồng ý sữ dụng cookie
Chúng tôi sử dụng cookie trên trang web này để phân tích lưu lượng truy cập, ghi nhớ tùy chọn của bạn và tối ưu hóa trải nghiệm của bạn.
Xem thêm
Oops!
Có vẻ như kết nối internet của bạn có vấn đề. Vui lòng kết nối lại và duyệt web.
AdBlock Detected!
Chúng tôi phát hiện bạn đang sử dụng plugin chặn quảng cáo trong trình duyệt của mình.
Doanh thu chúng tôi kiếm được từ quảng cáo được sử dụng để quản lý trang web này, chúng tôi yêu cầu bạn đưa trang web của chúng tôi vào danh sách trắng trong plugin chặn quảng cáo của bạn.
Site is Blocked
Sorry! This site is not available in your country.
Kỹ thuật số thế hệ tiếp theo Chào mừng bạn đến với trò chuyện WhatsApp
Xin chào! Chúng tôi có thể giúp gì cho bạn hôm nay?
Nhập vào đây...