PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED]Cut pieces out of long string.-VBForums
Results 1 to 6 of 6

Thread: [RESOLVED]Cut pieces out of long string.

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2008
    Posts
    128

    Exclamation [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 :
    Code:
    </span>
    Same goes for Coachman
    From:
    Code:
    <span class="family-name">
    To
    Code:
    </span>
    I dont really know how to do this..
    Hope some of you guys can help me with this
    Last edited by menzow; Apr 26th, 2010 at 05:33 PM. Reason: Thread resolved

  2. #2
    Fanatic Member FireXtol's Avatar
    Join Date
    Apr 2010
    Posts
    874

    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

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2008
    Posts
    128

    Re: Cut pieces out of long string.

    Quote Originally Posted by FireXtol View Post
    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

  4. #4
    Fanatic Member FireXtol's Avatar
    Join Date
    Apr 2010
    Posts
    874

    Re: Cut pieces out of long string.

    You'll require mid$() to read the actual string from the locations provided by InStr.

    vb Code:
    1. Private Function GetSpanByID(strHTML As String, strSpan As String) As String
    2. 'these are both taken byref, this makes a much more efficient 'pointer function'
    3. 'just be sure not to modify the values strHTML or strSpan, as the changes would be
    4. ' "back-propagated" to the variables you passed to this function
    5. 'this was done so a duplicate copy of the 'strHTML' will NOT be created every time this function is called
    6. 'assuming you're passing several kilobytes, maybe hundreds of kB as strHTML, this'll make a HUGE difference
    7.  
    8. Dim strFind As String, X As Long, Y As Long
    9.  
    10. strFind = "<span class=""" & strSpan & """>"
    11. X = InStr(strHTML, strFind)
    12. If X > 0 Then 'it found a match
    13.   Y = InStr(X + 1, strHTML, "</span>")
    14.   If Y > 0 Then
    15.     GetSpanByID = Mid$(strHTML, X + Len(strFind), Y - X - Len(strFind))
    16.   End If
    17. End If
    18. End Function
    19.  
    20. 'example usage:
    21.  
    22. 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>"
    23. Debug.Print GetSpanByID(htmlHere, "given-name")
    24. Debug.Print GetSpanByID(htmlHere, "additional-name")
    25. Debug.Print GetSpanByID(htmlHere, "family-name")
    26.  
    27. 'Or something like:
    28. dim strResult as String
    29. strResult = GetSpanByID(htmlHere, "family-name")
    30. if strResult <> vbNullString then ' found
    31.  
    32. Else 'not found
    33.  
    34. End If
    Last edited by FireXtol; Apr 26th, 2010 at 04:32 PM.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Apr 2008
    Posts
    128

    Re: Cut pieces out of long string.

    Quote Originally Posted by FireXtol View Post
    You'll require mid$() to read the actual string from the locations provided by InStr.

    vb Code:
    1. Private Function GetSpanByID(strHTML As String, strSpan As String) As String
    2. 'these are both taken byref, this makes a much more efficient 'pointer function'
    3. 'just be sure not to modify the values strHTML or strSpan, as the changes would be
    4. ' "back-propagated" to the variables you passed to this function
    5. 'this was done so a duplicate copy of the 'strHTML' will NOT be created every time this function is called
    6. 'assuming you're passing several kilobytes, maybe hundreds of kB as strHTML, this'll make a HUGE difference
    7.  
    8. Dim strFind As String, X As Long, Y As Long
    9.  
    10. strFind = "<span class=""" & strSpan & """>"
    11. X = InStr(strHTML, strFind)
    12. If X > 0 Then 'it found a match
    13.   Y = InStr(X + 1, strHTML, "</span>")
    14.   If Y > 0 Then
    15.     GetSpanByID = Mid$(strHTML, X + Len(strFind), Y - X - Len(strFind))
    16.   End If
    17. End If
    18. End Function
    19.  
    20. 'example usage:
    21.  
    22. 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>"
    23. Debug.Print GetSpanByID(htmlHere, "given-name")
    24. Debug.Print GetSpanByID(htmlHere, "additional-name")
    25. Debug.Print GetSpanByID(htmlHere, "family-name")
    26.  
    27. 'Or something like:
    28. dim strResult as String
    29. strResult = GetSpanByID(htmlHere, "family-name")
    30. if strResult <> vbNullString then ' found
    31.  
    32. Else 'not found
    33.  
    34. End If
    THANKS!

    Works perfect

  6. #6
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,132

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width