htmlutils.vb

来自「C#语言制作asp.net网上商店的」· VB 代码 · 共 403 行 · 第 1/2 页

VB
403
字号
Imports Microsoft.VisualBasic
Imports System.Net
Imports System.Web
Imports System.IO

Namespace NetShopForge.Common

    Public Class HtmlUtils







        Public Shared Function Clean(ByVal HTML As String, ByVal RemovePunctuation As Boolean) As String

            'First remove any HTML entities (  < etc)
            HTML = StripEntities(HTML, True)

            'Next remove any HTML Tags ("<....>")
            HTML = StripTags(HTML, True)

            'Finally remove any punctuation
            If RemovePunctuation Then
                HTML = StripPunctuation(HTML, True)
            End If

            Return HTML

        End Function

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Formats an Email address
        ''' </summary>
        ''' <param name="Email">The email address to format</param>
        ''' <returns>The formatted email address</returns>
        ''' <history>
        '''		[cnurse]	09/29/2005	moved from Globals
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Public Shared Function FormatEmail(ByVal Email As String) As String

            FormatEmail = ""

            If Not Email.Length = 0 Then
                If Trim(Email) <> "" Then
                    If Email.IndexOf("@") <> -1 Then
                        FormatEmail = "<a href=""mailto:" & Email & """>" & Email & "</a>"
                    Else
                        FormatEmail = Email
                    End If
                End If
            End If

            Return FormatEmail 'CloakText(FormatEmail)




        End Function

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' FormatText replaces <br/> tags by LineFeed characters
        ''' </summary>
        ''' <remarks>
        ''' </remarks>
        ''' <param name="HTML">The HTML content to clean up</param>
        ''' <returns>The cleaned up string</returns>
        ''' <history>
        '''		[cnurse]	12/13/2004	created
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Public Shared Function FormatText(ByVal HTML As String, ByVal RetainSpace As Boolean) As String

            'Match all variants of <br> tag (<br>, <BR>, <br/>, including embedded space
            Dim brMatch As String = "\s*<\s*[bB][rR]\s*/\s*>\s*"

            'Set up Replacement String
            Dim RepString As String
            If RetainSpace Then
                RepString = " "
            Else
                RepString = ""
            End If

            'Replace Tags by replacement String and return mofified string
            Return System.Text.RegularExpressions.Regex.Replace(HTML, brMatch, ControlChars.Lf)
        End Function

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Format a domain name including link
        ''' </summary>
        ''' <param name="Website">The domain name to format</param>
        ''' <returns>The formatted domain name</returns>
        ''' <history>
        '''		[cnurse]	09/29/2005	moved from Globals
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Public Shared Function FormatWebsite(ByVal Website As Object) As String

            FormatWebsite = ""

            If Not IsDBNull(Website) Then
                If Trim(Website.ToString()) <> "" Then
                    If Convert.ToBoolean(InStr(1, Website.ToString(), ".")) Then
                        FormatWebsite = "<a href=""" & IIf(Convert.ToBoolean(InStr(1, Website.ToString(), "://")), "", "http://").ToString & Website.ToString() & """>" & Website.ToString() & "</a>"
                    Else
                        FormatWebsite = Website.ToString()
                    End If
                End If
            End If

        End Function

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Shorten returns the first (x) characters of a string
        ''' </summary>
        ''' <remarks>
        ''' </remarks>
        ''' <param name="txt">The text to reduces</param>
        ''' <param name="length">The max number of characters to return</param>
        ''' <param name="suffix">An optional suffic to append to the shortened string</param>
        ''' <returns>The shortened string</returns>
        ''' <history>
        '''		[cnurse]	11/16/2004	created
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Public Shared Function Shorten(ByVal txt As String, ByVal length As Integer, ByVal suffix As String) As String
            Dim results As String
            If txt.Length > length Then
                results = txt.Substring(0, length) & suffix
            Else
                results = txt
            End If
            Return results
        End Function

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' StripEntities removes the HTML Entities from the content
        ''' </summary>
        ''' <remarks>
        ''' </remarks>
        ''' <param name="HTML">The HTML content to clean up</param>
        ''' <param name="RetainSpace">Indicates whether to replace the Entity by a space (true) or nothing (false)</param>
        ''' <returns>The cleaned up string</returns>
        ''' <history>
        '''		[cnurse]	11/16/2004	created
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Public Shared Function StripEntities(ByVal HTML As String, ByVal RetainSpace As Boolean) As String

            'Set up Replacement String
            Dim RepString As String
            If RetainSpace Then
                RepString = " "
            Else
                RepString = ""
            End If

            'Replace Entities by replacement String and return mofified string
            Return System.Text.RegularExpressions.Regex.Replace(HTML, "&[^;]*;", RepString)
        End Function

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' StripTags removes the HTML Tags from the content
        ''' </summary>
        ''' <remarks>
        ''' </remarks>
        ''' <param name="HTML">The HTML content to clean up</param>
        ''' <param name="RetainSpace">Indicates whether to replace the Tag by a space (true) or nothing (false)</param>
        ''' <returns>The cleaned up string</returns>
        ''' <history>
        '''		[cnurse]	11/16/2004	documented
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Public Shared Function StripTags(ByVal HTML As String, ByVal RetainSpace As Boolean) As String

            'Set up Replacement String
            Dim RepString As String
            If RetainSpace Then
                RepString = " "
            Else
                RepString = ""
            End If

            'Replace Tags by replacement String and return mofified string
            Return System.Text.RegularExpressions.Regex.Replace(HTML, "<[^>]*>", RepString)
        End Function

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' StripPunctuation removes the Punctuation from the content
        ''' </summary>
        ''' <remarks>
        ''' </remarks>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?