[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. 

PASSWORD UNZIP: HUNG.PRO.VN
Chúc Mọi Người Thành Công Với Thủ Thuật Trên.
Nếu mọi người có vướng mắc gì mình chia sẽ trên trang có thể gửi liên hê cho mình tại đây nhé.
Cảm ơn mọi người đã quan tâm.

No comments:

All Right Reserved © 2015 By Hung Pro VN