⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 htmlprocess.bas

📁 这是一本学习串口编程喝计算机监控的好书里面是用VB开发的源代码
💻 BAS
📖 第 1 页 / 共 2 页
字号:
Attribute VB_Name = "HtmlProcess"
Option Explicit
'depend on StringProcess.bas

Public Const cStrCharSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
Public Const cStrNumSet = "0123456789"
Public Const cEnMark = "~!@# $%^&*()_+|\=-`{}:<>?[];',./"  'Plus "
Public Const cCnMark = "~!-+·─.—、“”【 】:;‘’《》,。…?『』「」〈〉〔〕〖〗"
Public Const cEnToCnMark = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789` ̄!@#$%^&*()-_+\|[]{}' <>/"
Public Const cCnMarkSeg = ",。!;:、,.; :"
Public Const cIllegalFileChars = "\/:*?<>|"
Public Const cStrDigitSet = "0123456789+,-."
Public Const cCnNumSet1 = "0○零一二三四五六七八九十百千"
Public Const cCnNumSet2 = "零壹贰叁肆伍陆柒捌玖拾"

Public Function GetstrAllCharSet() As String
  GetstrAllCharSet = cStrCharSet + cStrNumSet + cEnMark + cCnMark + cEnToCnMark + Chr(&H22)
End Function

Public Function GetstrAllIllegalFileChars() As String
  GetstrAllIllegalFileChars = cIllegalFileChars + Chr(&H22)
End Function

Public Function FindNextTagUnit(ByVal strSource As String, lLocation As Long, strMarkFirst As String, strMarkSecond As String) As String
  'get string such as <html>
  Dim lTmp1 As Long
  Dim lTmp2 As Long
  Dim lStart As Long
  
  lStart = lLocation
  If lStart = 0 Then lStart = 1
  lTmp1 = InStr(lStart, LCase(strSource), strMarkFirst)
  
  If lTmp1 = 0 Then FindNextTagUnit = ""
  
  If Mid(strSource, lTmp1 + 1, 1) = "!" Then
    'process <!--    ....   -->
    lTmp2 = InStr(lStart, strSource, "--" + strMarkSecond)
    lTmp2 = lTmp2 + 2
  Else
    lTmp2 = InStr(lStart, LCase(strSource), strMarkSecond)
  End If
  
  If lTmp2 = 0 Then FindNextTagUnit = ""
  
  If lTmp1 >= 1 And lTmp2 > lTmp1 Then
    FindNextTagUnit = Mid(strSource, lTmp1, lTmp2 - lTmp1 + 1)
  End If
  
  If lTmp1 >= 1 And lTmp2 < lTmp1 Then
    lTmp2 = InStr(lTmp1 + 1, strSource, strMarkSecond)
    If lTmp2 < lTmp1 Then
      FindNextTagUnit = ""
    Else
      FindNextTagUnit = Mid(strSource, lTmp1, lTmp2 - lTmp1 + 1)
    End If
  End If
End Function

Public Function GetTagMark(ByVal strSource As String) As String
  'from <Script ...> or </Script> to Script
  Dim nLocation As Integer
  
  nLocation = InStr(1, strSource, " ")
  If nLocation = 0 Then
    GetTagMark = Mid(strSource, 2, Len(strSource) - 2)
  Else
    GetTagMark = Mid(strSource, 2, nLocation - 2)
  End If
End Function

Public Function SkipNoTagUnit(ByVal strSource As String, lLocation As Long, strMarkFirst As String, strMarkSecond As String, nTags As Integer) As Long
  Dim lTmp As Long
  Dim lTmpLoc As Long
  Dim I As Integer
  Dim strTmp As String
  
  lTmpLoc = lLocation
  If lTmpLoc < 1 Then lTmpLoc = 1
  
  For I = 1 To nTags
    strTmp = FindNextTagUnit(strSource, lTmpLoc, strMarkFirst, strMarkSecond)
    lTmp = Len(strTmp)
    If lTmp = 0 Then Exit For
    lTmp = InStr(lTmpLoc, LCase(strSource), LCase(strTmp))
    lTmpLoc = lTmp + Len(strTmp)
  Next I
  
  If I > nTags Then
    SkipNoTagUnit = lTmpLoc
  Else
    SkipNoTagUnit = 0             'There is no so much tags.
  End If
End Function

Public Function FindTagUnit(ByVal strSource As String, lLocation As Long, strMark As String) As String
  Dim lStart As Long
  Dim lLen As Long
  Dim lTmpLoc As Long
  Dim strTag As String
  
  lStart = lLocation
  lLen = Len(strSource)
    
  Do While lStart < lLen
    strTag = FindNextTagUnit(strSource, lStart, "<", ">")
    If strTag = "" Then
      FindTagUnit = ""
      Exit Function
    End If
    
    If strMark = "!--" And InStr(1, strTag, "!--") <> 0 Then
      FindTagUnit = strTag
      Exit Function
    End If
    
    If InStr(1, LCase(strTag), LCase("<" + strMark + ">")) > 0 Or _
       InStr(1, LCase(strTag), LCase("<" + strMark + " ")) > 0 Or _
       InStr(1, LCase(strTag), LCase("</" + strMark + ">")) > 0 Then
      FindTagUnit = strTag
      Exit Function
    Else
      lStart = InStr(lStart, strSource, strTag) + Len(strTag)
    End If
  Loop
  
  FindTagUnit = ""
End Function

Public Function FindStartTagUnit(ByVal strSource As String, lLocation As Long, strMark As String) As String
  Dim lStart As Long
  Dim lLen As Long
  Dim strTag As String
  
  lStart = lLocation
  If lStart = 0 Then lStart = 1
  lLen = Len(strSource)
  
  Do While lStart < lLen
    If strMark <> "" Then
      strTag = FindTagUnit(strSource, lStart, strMark)
    Else
      strTag = FindNextTagUnit(strSource, lStart, "<", ">")
    End If
    
    If strTag = "" Then
      FindStartTagUnit = ""
      Exit Function
    End If
    
    If Mid(strTag, 2, 1) <> "/" Then
      FindStartTagUnit = strTag
      Exit Function
    Else
      lStart = InStr(lStart, LCase(strSource), LCase(strTag)) + Len(strTag)
    End If
    
    DoEvents
  Loop
  
  FindStartTagUnit = ""
End Function
  
Public Function FindEndTagUnit(ByVal strSource As String, lLocation As Long, strMark As String) As String
  Dim lStart As Long
  Dim lLen As Long
  Dim strTag As String
  
  lStart = lLocation
  lLen = Len(strSource)
  
  Do While lStart < lLen
    If strMark <> "" Then
      strTag = FindTagUnit(strSource, lStart, strMark)
    Else
      strTag = FindNextTagUnit(strSource, lStart, "<", ">")
    End If
    
    If strTag = "" Then
      FindEndTagUnit = ""
      Exit Function
    End If
    
    If Mid(strTag, 2, 1) = "/" Then
      FindEndTagUnit = strTag
      Exit Function
    Else
      lStart = InStr(lStart, LCase(strSource), LCase(strTag)) + Len(strTag)
    End If
  Loop
  
  FindEndTagUnit = ""
End Function

Public Function PeekInfoBetweenTwins(ByVal strSource As String, lLocation As Long, strMark As String) As String
  Dim lTmpStart As Long
  Dim lTmpEnd As Long
  Dim strTmp As String
  
  strTmp = FindStartTagUnit(strSource, lLocation, strMark)
  If strTmp = "" Then
    PeekInfoBetweenTwins = ""
    Exit Function
  End If
  lTmpStart = InStr(lLocation, LCase(strSource), LCase(strTmp))
  lTmpStart = lTmpStart + Len(strTmp)
  
  strTmp = FindEndTagUnit(strSource, lTmpStart, strMark)
  If strTmp <> "" Then
    lTmpEnd = InStr(lTmpStart, LCase(strSource), LCase(strTmp))
    PeekInfoBetweenTwins = Mid(strSource, lTmpStart, lTmpEnd - lTmpStart)
  Else
    strTmp = FindStartTagUnit(strSource, lTmpStart, strMark)
    If strTmp <> "" Then        'only <p>, without </p>
      lTmpEnd = InStr(lTmpStart, LCase(strSource), LCase(strTmp))
      PeekInfoBetweenTwins = Mid(strSource, lTmpStart, lTmpEnd - lTmpStart)
    Else                        'get the last paragragh
      strTmp = FindNextTagUnit(strSource, lTmpStart, "<", ">")
      If strTmp <> "" Then
        lTmpEnd = InStr(lTmpStart, LCase(strSource), LCase(strTmp))
        PeekInfoBetweenTwins = Mid(strSource, lTmpStart, lTmpEnd - lTmpStart)
      Else
        PeekInfoBetweenTwins = ""
      End If
    End If
  End If
  PeekInfoBetweenTwins = Trim(PeekInfoBetweenTwins)
End Function

Public Function PeekAllBetweenTwins(ByVal strSource As String, lLocation As Long, strMark As String) As String
  'include start and end tags
  Dim lTmpStart As Long
  Dim lTmpEnd As Long
  Dim strTmp As String
  Dim strStartTag As String
  Dim strEndTag As String
  
  strTmp = FindStartTagUnit(strSource, lLocation, strMark)
  strStartTag = strTmp
  If strTmp = "" Then
    PeekAllBetweenTwins = ""
    Exit Function
  End If
  lTmpStart = InStr(lLocation, LCase(strSource), LCase(strTmp))
  lTmpStart = lTmpStart + Len(strTmp)
  
  strTmp = FindEndTagUnit(strSource, lTmpStart, strMark)
  strEndTag = strTmp
  If strTmp <> "" Then
    lTmpEnd = InStr(lTmpStart, LCase(strSource), LCase(strTmp))
    PeekAllBetweenTwins = strStartTag + Mid(strSource, lTmpStart, lTmpEnd - lTmpStart) + strEndTag
  Else
    PeekAllBetweenTwins = ""
  End If
  PeekAllBetweenTwins = Trim(PeekAllBetweenTwins)
End Function

Public Function GetHtmlHead(ByVal strSource As String) As String
  Dim strBorderMark As String
  Dim nTmp As Integer
  
  strBorderMark = vbCrLf + vbCrLf
  nTmp = InStr(1, LCase(strSource), LCase(strBorderMark))
  If nTmp > 500 Or nTmp = 0 Then
    GetHtmlHead = ""
  Else
    GetHtmlHead = Mid(strSource, 1, nTmp - 1)
  End If
End Function

Public Function GetHtmlContent(ByVal strSource As String) As String
  Dim strBorderMark As String
  Dim nTmp As Integer
  
  strBorderMark = vbCrLf + vbCrLf
  nTmp = InStr(1, strSource, strBorderMark)
  If nTmp > 500 Or nTmp = 0 Then
    GetHtmlContent = ""
  Else
    GetHtmlContent = Mid(strSource, nTmp + 4)
  End If
End Function

Public Function GetHtmlLength(ByVal strSource As String) As Long
  'strSource is all the data, include head and content.
  'sometimes there is no <html>, sometimes there several </html>
  'so the Function is not very valid!
  Dim lReal As Long
  Dim lHtmlStart As Long
  Dim lHtmlEnd As Long
  
  lReal = Len(GetHtmlContent(strSource))
  lHtmlStart = InStr(1, LCase(strSource), "<html>")
  lHtmlEnd = InStr(1, LCase(strSource), "</html>")
  
  If lHtmlStart > 0 And lHtmlEnd > lHtmlStart Then
    GetHtmlLength = lReal
  Else
    GetHtmlLength = 0
  End If
End Function

Public Function GetResponseHeadLength(ByVal strSource As String) As Integer
  Dim strTmp As String
  strTmp = GetHtmlHead(strSource)
  GetResponseHeadLength = Len(strTmp)
End Function

Public Function GetCurrentInfo(ByVal strSource As String, lLocation As Long) As String
  'Get information between ">" and "<", that is ">" Information "<"
  Dim lStart As Long
  Dim lEnd As Long
  Dim lTmp As Long
  Dim strTag As String
  
  lStart = lLocation
  If lStart < 1 Then lStart = 1
  lTmp = lStart
  
  Do While lTmp > 0
    If Mid(strSource, lTmp, 1) = ">" Then
      lStart = lTmp + 1
      Exit Do
    Else
      lTmp = lTmp - 1
    End If
  Loop
  
  strTag = FindNextTagUnit(strSource, lStart, "<", ">")
  If strTag <> "" Then
    lEnd = InStr(lStart, LCase(strSource), LCase(strTag))
    GetCurrentInfo = Mid(strSource, lStart, lEnd - lStart)
  Else
    GetCurrentInfo = Mid(strSource, lStart)
  End If
End Function

Public Function GetHttpFromTag(ByVal strTag As String) As String
  Dim strTmp As String
  Dim nTmp As Integer
  strTmp = strTag
  nTmp = InStr(1, LCase(strTmp), "http://")
  If nTmp > 0 Then
    nTmp = nTmp - 2
    strTmp = Mid(strTmp, nTmp)
    GetHttpFromTag = GetInsideString(strTmp, Chr(&H22))
  Else
    nTmp = InStr(1, LCase(strTmp), "href=")
    If nTmp > 0 Then
      nTmp = nTmp + 2
      strTmp = Mid(strTmp, nTmp)
      GetHttpFromTag = GetInsideString(strTmp, Chr(&H22))
    Else
      GetHttpFromTag = ""
    End If
  End If
End Function

Public Function DelAllTags(ByVal strSource As String, strMark As String) As String
  Dim strTmp As String
  Dim strTag As String
  Dim lStart As Long
  
  strTmp = strSource
  lStart = 1
  If LCase(strMark) = "style" Or LCase(strMark) = "script" Then
    strTag = PeekAllBetweenTwins(strTmp, lStart, strMark)
  Else
    If strMark <> "" Then
      strTag = FindTagUnit(strTmp, lStart, strMark)
    Else
      strTag = FindNextTagUnit(strTmp, lStart, "<", ">")
    End If
  End If
  
  Do While strTag <> ""
    lStart = InStr(1, strTmp, strTag)   'need not lcase or ucase
    strTmp = Mid(strTmp, 1, lStart - 1) + Mid(strTmp, lStart + Len(strTag))
    
    If LCase(strMark) = "style" Or LCase(strMark) = "script" Then
      strTag = PeekAllBetweenTwins(strTmp, lStart, strMark)
    Else
      If strMark <> "" Then
        strTag = FindTagUnit(strTmp, lStart, strMark)
      Else
        strTag = FindNextTagUnit(strTmp, lStart, "<", ">")
      End If
    End If
  Loop
  
  DelAllTags = strTmp
End Function

Public Function DelAllCrLf(ByVal strSource As String) As String
  Dim strTmp As String
  Dim lStart As Long
  
  strTmp = strSource
  
  strTmp = DelAllSubChars(strTmp, Chr(&HA))
  strTmp = DelAllSubChars(strTmp, Chr(&HD))
  
  DelAllCrLf = strTmp
End Function

Public Function GetCenterTitle(ByVal strSource As String) As String
  Dim strTmp As String
  Dim strTag As String
  Dim strTitle As String

⌨️ 快捷键说明

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