[RESOLVED]Cut pieces out of long string.
Hey,
I am kinda stuck at a program im developing.
I am trying to get 2 pieces of data out of html code.
Code:
<h4><span class="fn n"><span class="given-name">Debbie</span> <span class="additional-name">T.</span> <span class="family-name">Coachman</span></span></h4>
Ok this is what i have
I want to cut out: Debbie
And Couchman
What i thought was
Search for this :
Code:
<span class="given-name">
Start the selection and stop when it hits :
Same goes for Coachman
From:
Code:
<span class="family-name">
To
I dont really know how to do this..
Hope some of you guys can help me with this
Re: Cut pieces out of long string.
Use InStr(), it returns the location of the first character matched.
Code:
x = InStr(htmlHere, "<span class-""given-name"">")
if x > 0 then do_something 'it found a match
'to find the next result, the end of the field:
x = InStr(x+1, htmlHere, "</span>") 'here I use X+1 as a starting point, it won't search before this location, only after
'do this over again for the family-name
Re: Cut pieces out of long string.
Quote:
Originally Posted by
FireXtol
Use InStr(), it returns the location of the first character matched.
Code:
x = InStr(htmlHere, "<span class-""given-name"">")
if x > 0 then do_something 'it found a match
'to find the next result, the end of the field:
x = InStr(x+1, htmlHere, "</span>") 'here I use X+1 as a starting point, it won't search before this location, only after
'do this over again for the family-name
Sorry but im not following this :(
Lets say i try to do this
list1.additem x
it adds a 0
I want it do output debbie.
Still trying to figure it out
Thanks for the quick reply btw ;)
Re: Cut pieces out of long string.
You'll require mid$() to read the actual string from the locations provided by InStr.
vb Code:
Private Function GetSpanByID(strHTML As String, strSpan As String) As String
'these are both taken byref, this makes a much more efficient 'pointer function'
'just be sure not to modify the values strHTML or strSpan, as the changes would be
' "back-propagated" to the variables you passed to this function
'this was done so a duplicate copy of the 'strHTML' will NOT be created every time this function is called
'assuming you're passing several kilobytes, maybe hundreds of kB as strHTML, this'll make a HUGE difference
Dim strFind As String, X As Long, Y As Long
strFind = "<span class=""" & strSpan & """>"
X = InStr(strHTML, strFind)
If X > 0 Then 'it found a match
Y = InStr(X + 1, strHTML, "</span>")
If Y > 0 Then
GetSpanByID = Mid$(strHTML, X + Len(strFind), Y - X - Len(strFind))
End If
End If
End Function
'example usage:
htmlHere = "<h4><span class=""fn n""><span class=""given-name"">Debbie</span> <span class=""additional-name"">T.</span> <span class=""family-name"">Coachman</span></span></h4>"
Debug.Print GetSpanByID(htmlHere, "given-name")
Debug.Print GetSpanByID(htmlHere, "additional-name")
Debug.Print GetSpanByID(htmlHere, "family-name")
'Or something like:
dim strResult as String
strResult = GetSpanByID(htmlHere, "family-name")
if strResult <> vbNullString then ' found
Else 'not found
End If
Re: Cut pieces out of long string.
Quote:
Originally Posted by
FireXtol
You'll require mid$() to read the actual string from the locations provided by InStr.
vb Code:
Private Function GetSpanByID(strHTML As String, strSpan As String) As String
'these are both taken byref, this makes a much more efficient 'pointer function'
'just be sure not to modify the values strHTML or strSpan, as the changes would be
' "back-propagated" to the variables you passed to this function
'this was done so a duplicate copy of the 'strHTML' will NOT be created every time this function is called
'assuming you're passing several kilobytes, maybe hundreds of kB as strHTML, this'll make a HUGE difference
Dim strFind As String, X As Long, Y As Long
strFind = "<span class=""" & strSpan & """>"
X = InStr(strHTML, strFind)
If X > 0 Then 'it found a match
Y = InStr(X + 1, strHTML, "</span>")
If Y > 0 Then
GetSpanByID = Mid$(strHTML, X + Len(strFind), Y - X - Len(strFind))
End If
End If
End Function
'example usage:
htmlHere = "<h4><span class=""fn n""><span class=""given-name"">Debbie</span> <span class=""additional-name"">T.</span> <span class=""family-name"">Coachman</span></span></h4>"
Debug.Print GetSpanByID(htmlHere, "given-name")
Debug.Print GetSpanByID(htmlHere, "additional-name")
Debug.Print GetSpanByID(htmlHere, "family-name")
'Or something like:
dim strResult as String
strResult = GetSpanByID(htmlHere, "family-name")
if strResult <> vbNullString then ' found
Else 'not found
End If
THANKS!
Works perfect :eek2:
Re: [RESOLVED]Cut pieces out of long string.
I see this is resolved but you can use the Microsoft HTML object library to do it also. Just make sure you set the correct reference.
Code:
Private Sub Command1_Click()
Dim objHTML As MSHTML.HTMLDocument
Dim objSpan As HTMLSpanElement
Dim objAttrib As Object
Dim strHTML As String
strHTML = "<h4><span class='fn n'><span class='given-name'>Debbie</span>"
strHTML = strHTML & "<span class='additional-name'>T.</span>"
strHTML = strHTML & "<span class='family-name'>Coachman</span></span></h4>"
Set objHTML = New MSHTML.HTMLDocument
objHTML.body.innerHTML = strHTML
For Each objSpan In objHTML.getElementsByTagName("span")
Set objAttrib = objSpan.getAttributeNode("class")
If Not objAttrib Is Nothing Then
If objAttrib.nodeValue = "given-name" Then
Debug.Print "Given Name: " & objSpan.innerText
End If
If objAttrib.nodeValue = "family-name" Then
Debug.Print "Family Name: " & objSpan.innerText
End If
End If
Next
If Not objAttrib Is Nothing Then
Set objAttrib = Nothing
End If
If Not objSpan Is Nothing Then
Set objSpan = Nothing
End If
If Not objHTML Is Nothing Then
Set objHTML = Nothing
End If
End Sub