by Jamie Richard Wilson (2 Submissions)
Category: String Manipulation
Compatability: Visual Basic 3.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (5 Votes)
This is a set of three functions that pull tagged data, such as that from HTML or XML, based on the tag name. I've used this in a number of applications where I've need to store multiple bits of variable-length data in a single string or file.
Inputs
A string containing tags which in turn contain data you are searching for AND the name of the tag.
For instance, if you're pulling from HTML for anything between
Public Function GetTag(SourceString As String, Tag As String) As String
'Gets the tag and text between it
If InStr(SourceString, "<" & Tag & ">") = 0 Then
GetTag = ""
Exit Function
End If
GetTag = Mid$(SourceString, InStr(SourceString, "<" & Tag & ">"), InStr(SourceString, "") + Len("") - 1)
End Function
Public Function GetTagText(SourceString As String, Tag As String) As String
'Grabs the text between tags
If InStr(SourceString, "<" & Tag & ">") = 0 Then
GetTagText = ""
Exit Function
End If
GetTagText = Mid$(SourceString, InStr(SourceString, "<" & Tag & ">") + Len("<" & Tag & ">"), (InStr(SourceString, "")) - (InStr(SourceString, "<" & Tag & ">") + Len("<" & Tag & ">")))
End Function
Public Function CutTag(SourceString As String, Tag As String) As String
'Cuts the entire tag out of the text
If InStr(SourceString, "<" & Tag & ">") = 0 Then
CutTag = ""
Exit Function
End If
CutTag = Left$(SourceString, InStr(SourceString, "<" & Tag & ">") - 1) & Mid$(SourceString, InStrRev(SourceString, "") + Len(""))
End Function