Results 1 to 12 of 12

Thread: get text between text

  1. #1

    Thread Starter
    Banned
    Join Date
    Feb 2009
    Posts
    15

    get text between text

    my app gets the source from a webpage, the source includes the following info:

    Code:
    <area id="a_0_1" shape="poly" coords="89, 117, 126, 137, 89, 157, 52, 137" title="" href="karte.php?d=279938&c=7d" />
    <area id="a_0_2" shape="poly" coords="125, 97, 162, 117, 125, 137, 88, 117" title="I. Apollo" href="karte.php?d=279137&c=ca" />
    <area id="a_0_3" shape="poly" coords="161, 77, 198, 97, 161, 117, 124, 97" title="" href="karte.php?d=278336&c=73" />
    <area id="a_0_4" shape="poly" coords="197, 57, 234, 77, 197, 97, 160, 77" title="1)Cegóvia" href="karte.php?d=277535&c=81" />
    <area id="a_0_5" shape="poly" coords="233, 37, 270, 57, 233, 77, 196, 57" title="" href="karte.php?d=276734&c=7a" />
    <area id="a_0_6" shape="poly" coords="269, 17, 306, 37, 269, 57, 232, 37" title="Lietuvaitis.LtD" href="karte.php?d=275933&c=50" />
    <area id="a_1_0" shape="poly" coords="90, 157, 127, 177, 90, 197, 53, 177" title="[1]" href="karte.php?d=280740&c=63" />
    <area id="a_1_1" shape="poly" coords="126, 137, 163, 157, 126, 177, 89, 157" title="Ellesmèra" href="karte.php?d=279939&c=51" />
    <area id="a_1_2" shape="poly" coords="162, 117, 199, 137, 162, 157, 125, 137" title="" href="karte.php?d=279138&c=f1" />
    <area id="a_1_3" shape="poly" coords="198, 97, 235, 117, 198, 137, 161, 117" title="" href="karte.php?d=278337&c=9c" />
    <area id="a_1_4" shape="poly" coords="234, 77, 271, 97, 234, 117, 197, 97" title="Zwijnaarde" href="karte.php?d=277536&c=80" />
    <area id="a_1_5" shape="poly" coords="270, 57, 307, 77, 270, 97, 233, 77" title="" href="karte.php?d=276735&c=f0" />
    <area id="a_1_6" shape="poly" coords="306, 37, 343, 57, 306, 77, 269, 57" title="" href="karte.php?d=275934&c=a6" />
    <area id="a_2_0" shape="poly" coords="127, 177, 164, 197, 127, 217, 90, 197" title="" href="karte.php?d=280741&c=37" />
    <area id="a_2_1" shape="poly" coords="163, 157, 200, 177, 163, 197, 126, 177" title="shadow alliance" href="karte.php?d=279940&c=01" />
    <area id="a_2_2" shape="poly" coords="199, 137, 236, 157, 199, 177, 162, 157" title="" href="karte.php?d=279139&c=c7" />
    <area id="a_2_3" shape="poly" coords="235, 117, 272, 137, 235, 157, 198, 137" title="" href="karte.php?d=278338&c=a2" />
    
    <area id="a_2_4" shape="poly" coords="271, 97, 308, 117, 271, 137, 234, 117" title="" href="karte.php?d=277537&c=93" />
    <area id="a_2_5" shape="poly" coords="307, 77, 344, 97, 307, 117, 270, 97" title="" href="karte.php?d=276736&c=f3" />
    <area id="a_2_6" shape="poly" coords="343, 57, 380, 77, 343, 97, 306, 77" title="" href="karte.php?d=275935&c=2f" />
    <area id="a_3_0" shape="poly" coords="164, 197, 201, 217, 164, 237, 127, 217" title="robskies dorp 1" href="karte.php?d=280742&c=78" />
    <area id="a_3_1" shape="poly" coords="200, 177, 237, 197, 200, 217, 163, 197" title="" href="karte.php?d=279941&c=35" />
    <area id="a_3_2" shape="poly" coords="236, 157, 273, 177, 236, 197, 199, 177" title="" href="karte.php?d=279140&c=49" />
    <area id="a_3_3" shape="poly" coords="272, 137, 309, 157, 272, 177, 235, 157" title="[01] HQ" href="karte.php?d=278339&c=8c" />
    <area id="a_3_4" shape="poly" coords="308, 117, 345, 137, 308, 157, 271, 137" title="" href="karte.php?d=277538&c=bb" />
    <area id="a_3_5" shape="poly" coords="344, 97, 381, 117, 344, 137, 307, 117" title="Verlaten Vallei" href="karte.php?d=276737&c=55" />
    <area id="a_3_6" shape="poly" coords="380, 77, 417, 97, 380, 117, 343, 97" title="" href="karte.php?d=275936&c=65" />
    <area id="a_4_0" shape="poly" coords="201, 217, 238, 237, 201, 257, 164, 237" title="Ozymandias I" href="karte.php?d=280743&c=95" />
    <area id="a_4_1" shape="poly" coords="237, 197, 274, 217, 237, 237, 200, 217" title="" href="karte.php?d=279942&c=e3" />
    <area id="a_4_2" shape="poly" coords="273, 177, 310, 197, 273, 217, 236, 197" title="" href="karte.php?d=279141&c=08" />
    <area id="a_4_3" shape="poly" coords="309, 157, 346, 177, 309, 197, 272, 177" title="" href="karte.php?d=278340&c=fc" />
    <area id="a_4_4" shape="poly" coords="345, 137, 382, 157, 345, 177, 308, 157" title="" href="karte.php?d=277539&c=3f" />
    <area id="a_4_5" shape="poly" coords="381, 117, 418, 137, 381, 157, 344, 137" title="" href="karte.php?d=276738&c=83" />
    <area id="a_4_6" shape="poly" coords="417, 97, 454, 117, 417, 137, 380, 117" title="" href="karte.php?d=275937&c=d9" />
    
    <area id="a_5_0" shape="poly" coords="238, 237, 275, 257, 238, 277, 201, 257" title="" href="karte.php?d=280744&c=75" />
    <area id="a_5_1" shape="poly" coords="274, 217, 311, 237, 274, 257, 237, 237" title="HayaleT" href="karte.php?d=279943&c=30" />
    <area id="a_5_2" shape="poly" coords="310, 197, 347, 217, 310, 237, 273, 217" title="Latium" href="karte.php?d=279142&c=f2" />
    <area id="a_5_3" shape="poly" coords="346, 177, 383, 197, 346, 217, 309, 197" title="" href="karte.php?d=278341&c=18" />
    <area id="a_5_4" shape="poly" coords="382, 157, 419, 177, 382, 197, 345, 177" title="" href="karte.php?d=277540&c=b8" />
    <area id="a_5_5" shape="poly" coords="418, 137, 455, 157, 418, 177, 381, 157" title="spirithoes´s Dorp" href="karte.php?d=276739&c=30" />
    <area id="a_5_6" shape="poly" coords="454, 117, 491, 137, 454, 157, 417, 137" title="" href="karte.php?d=275938&c=4f" />
    <area id="a_6_0" shape="poly" coords="275, 257, 312, 277, 275, 297, 238, 277" title="" href="karte.php?d=280745&c=f3" />
    <area id="a_6_1" shape="poly" coords="311, 237, 348, 257, 311, 277, 274, 257" title="spookieee" href="karte.php?d=279944&c=7a" />
    <area id="a_6_2" shape="poly" coords="347, 217, 384, 237, 347, 257, 310, 237" title="" href="karte.php?d=279143&c=ce" />
    <area id="a_6_3" shape="poly" coords="383, 197, 420, 217, 383, 237, 346, 217" title="" href="karte.php?d=278342&c=35" />
    <area id="a_6_4" shape="poly" coords="419, 177, 456, 197, 419, 217, 382, 197" title="" href="karte.php?d=277541&c=0c" />
    <area id="a_6_5" shape="poly" coords="455, 157, 492, 177, 455, 197, 418, 177" title="" href="karte.php?d=276740&c=ac" />
    now i want my app to get the numbers between href="karte.php?d= and &c
    and put it into text1.text.

    how can i do this? i tried with a function called getbetween but this one only gives the first result.

    thanks

  2. #2
    Frenzied Member some1uk03's Avatar
    Join Date
    Jun 2006
    Location
    London, UK
    Posts
    1,675

    Re: get text between text

    Use instr() to get the position of where href begins.. then the rest is simple and straightforward..

    Use mid(), right() functions to extract the data...
    _____________________________________________________________________

    ----If this post has helped you. Please take time to Rate it.
    ----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.



  3. #3
    Addicted Member
    Join Date
    Aug 2008
    Posts
    136

    Re: get text between text

    maybe you can manipulate those string first. you make the string to be href="karte.php?d= 279944 &c=7a" (add spaces) so you can get the string between "href="karte.php?d= " and " &c=7a" using GetBetween function..

  4. #4
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: get text between text

    Code:
    Option Explicit
    
    Public Function Between(Text As String, Before As String, After As String, Output() As String) As Long
        Dim lngA As Long, lngB As Long, lngC As Long
        Erase Output
        If (LenB(Before) > 0) And (LenB(After) > 0) And (LenB(Text) > 0) Then
            ' reserve maximum possible items
            ReDim Output(LenB(Text) \ (LenB(Before) + LenB(After)))
            ' find first
            lngA = InStrB(Text, Before)
            Do While lngA
                If lngA And 1 Then
                    Do
                        ' find after
                        lngB = InStrB(lngA + LenB(Before), Text, After)
                        If lngB And 1 Then
                            ' found both, store what we got
                            Output(lngC) = MidB$(Text, lngA + LenB(Before), lngB - lngA - LenB(Before))
                            lngC = lngC + 1
                            ' find next
                            lngA = InStrB(lngB + LenB(After), Text, Before)
                            ' exit inner loop
                            Exit Do
                        ElseIf lngB > 0 Then
                            ' invalid position, not beginning of a character, try next
                            lngB = InStrB(lngB + 1, Text, After)
                        Else
                            ' exit both loops
                            lngA = 0
                            Exit Do
                        End If
                    Loop While lngB
                ElseIf lngA > 0 Then
                    ' invalid position, not beginning of a character, try next
                    lngA = InStrB(lngA + 1, Text, Before)
                End If
            Loop
            ' we are done, just need to clean up memory
            If (lngC > 0) Then
                ' remove extra items
                ReDim Preserve Output(lngC - 1)
                ' return number of items found
                Between = lngC
            Else
                ' return empty array (LBound = 0, UBound = -1)
                Output = Split(vbNullString)
            End If
        End If
    End Function
    
    Private Sub Form_Load()
        Dim strText As String, strData() As String
        Dim lngA As Long
        ' open data from file
        Open "C:\data.txt" For Input As #1
            strText = Input(LOF(1), #1)
        Close #1
        ' if we get more than none...
        For lngA = 0 To Between(strText, "href=""karte.php?d=", "&c", strData) - 1
            ' print everything we got
            Debug.Print strData(lngA)
        Next lngA
    End Sub
    Yes, this was easy enough for me to do, otherwise wouldn't have bothered


    Edit!
    Updated Strings.bas in my public Modules folder.
    Last edited by Merri; May 24th, 2009 at 06:49 AM.

  5. #5
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: get text between text

    Perhaps use Instr() or InstrB() can save a few miliseconds, but this is a shortest and simple way (7 lines of main code):
    Code:
    Sub GetArrayBetween()
       Const Tag1 = "href=""karte.php?d="
       Const Tag2 = "&c"
       Dim sItem() As String, i As Long
       
    1  Open "C:\VBF\data.txt" For Input As #1
    2  sItem = Split(Input(LOF(1), 1), Tag1)
    3  Close #1
       '-- extract each number and use
    4  For i = 1 To UBound(sItem) '-- (i - 1) is used to remove the first item (0)
    5     sItem(i - 1) = Split(sItem(i), Tag2, 2)(0)
    6  Next
    7  ReDim Preserve sItem(UBound(sItem) - 1) '-- one item short
       
       For i = 0 To UBound(sItem)
          Debug.Print sItem(i)
       Next
    End Sub
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  6. #6

    Thread Starter
    Banned
    Join Date
    Feb 2009
    Posts
    15

    Re: get text between text

    Quote Originally Posted by Merri View Post
    Code:
    Option Explicit
    
    Public Function Between(Text As String, Before As String, After As String, Output() As String) As Long
        Dim lngA As Long, lngB As Long, lngC As Long
        Erase Output
        If (LenB(Before) > 0) And (LenB(After) > 0) And (LenB(Text) > 0) Then
            ' reserve maximum possible items
            ReDim Output(LenB(Text) \ (LenB(Before) + LenB(After)))
            ' find first
            lngA = InStrB(Text, Before)
            Do While lngA
                If lngA And 1 Then
                    Do
                        ' find after
                        lngB = InStrB(lngA + LenB(Before), Text, After)
                        If lngB And 1 Then
                            ' found both, store what we got
                            Output(lngC) = MidB$(Text, lngA + LenB(Before), lngB - lngA - LenB(Before))
                            lngC = lngC + 1
                            ' find next
                            lngA = InStrB(lngB + LenB(After), Text, Before)
                            ' exit inner loop
                            Exit Do
                        ElseIf lngB > 0 Then
                            ' invalid position, not beginning of a character, try next
                            lngB = InStrB(lngB + 1, Text, After)
                        Else
                            ' exit both loops
                            lngA = 0
                            Exit Do
                        End If
                    Loop While lngB
                ElseIf lngA > 0 Then
                    ' invalid position, not beginning of a character, try next
                    lngA = InStrB(lngA + 1, Text, Before)
                End If
            Loop
            ' we are done, just need to clean up memory
            If (lngC > 0) Then
                ' remove extra items
                ReDim Preserve Output(lngC - 1)
                ' return number of items found
                Between = lngC
            Else
                ' return empty array (LBound = 0, UBound = -1)
                Output = Split(vbNullString)
            End If
        End If
    End Function
    
    Private Sub Form_Load()
        Dim strText As String, strData() As String
        Dim lngA As Long
        ' open data from file
        Open "C:\data.txt" For Input As #1
            strText = Input(LOF(1), #1)
        Close #1
        ' if we get more than none...
        For lngA = 0 To Between(strText, "href=""karte.php?d=", "&c", strData) - 1
            ' print everything we got
            Debug.Print strData(lngA)
        Next lngA
    End Sub
    Yes, this was easy enough for me to do, otherwise wouldn't have bothered


    Edit!
    Updated Strings.bas in my public Modules folder.
    this worked great for me, thanks

  7. #7

    Thread Starter
    Banned
    Join Date
    Feb 2009
    Posts
    15

    Re: get text between text

    Quote Originally Posted by anhn View Post
    Perhaps use Instr() or InstrB() can save a few miliseconds, but this is a shortest and simple way (7 lines of main code):
    Code:
    Sub GetArrayBetween()
       Const Tag1 = "href=""karte.php?d="
       Const Tag2 = "&c"
       Dim sItem() As String, i As Long
       
    1  Open "C:\VBF\data.txt" For Input As #1
    2  sItem = Split(Input(LOF(1), 1), Tag1)
    3  Close #1
       '-- extract each number and use
    4  For i = 1 To UBound(sItem) '-- (i - 1) is used to remove the first item (0)
    5     sItem(i - 1) = Split(sItem(i), Tag2, 2)(0)
    6  Next
    7  ReDim Preserve sItem(UBound(sItem) - 1) '-- one item short
       
       For i = 0 To UBound(sItem)
          Debug.Print sItem(i)
       Next
    End Sub
    thanks for the shorter version
    i guess i will use this cause this one also is faster.

  8. #8

    Thread Starter
    Banned
    Join Date
    Feb 2009
    Posts
    15

    Re: get text between text

    ive got a new small problem wich i hope you can help me with.
    now it gives al the numbers, but it would be better (to make my app faster) if it only gives the numbers where "tilte" is not empty.
    hopefully you can also help me with this

    Thanks

    ps youre great

  9. #9
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: get text between text

    Just to point it out, anhn's code will give invalid results if there are more of the first tag than the last tag. It would need to check whether the UBound of the second tag Split is 1. If it is 0, no second tag was found and thus it should not be added into the array.



    Edit!
    Updating for the title parsing:
    Code:
    Private Sub Form_Load()
        Dim strText As String, strData() As String, strRow() As String
        Dim lngA As Long
        ' open data from file
        Open "C:\data.txt" For Input As #1
            strText = Input(LOF(1), #1)
        Close #1
        ' if we get more than none...
        For lngA = 0 To Between(strText, " title=""", "&c", strData) - 1
            strRow = Split(strData(lngA), """ href=""karte.php?d=", 2)
            If LenB(strRow(0)) Then Debug.Print strRow(1), strRow(0)
        Next lngA
    End Sub


    Edit #2!
    anhn's suggestion functionalized and fixed the issue:
    Code:
    Public Function Between(Text As String, Before As String, After As String, Output() As String) As Long
        Dim lngA As Long, strBetween() As String
        Output = Split(Text, Before)
        For lngA = 1 To UBound(Output)
            strBetween = Split(Output(lngA), After, 2)
            If UBound(strBetween) = 1 Then
                Output(Between) = strBetween(0)
                Between = Between + 1
            End If
        Next lngA
        If Between > 0 Then
            ReDim Preserve Output(Between - 1)
        Else
            Output = Split(vbNullString)
        End If
    End Function
    Last edited by Merri; May 24th, 2009 at 09:05 AM.

  10. #10
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: get text between text

    Quote Originally Posted by Merri View Post
    Just to point it out, anhn's code will give invalid results if there are more of the first tag than the last tag. It would need to check whether the UBound of the second tag Split is 1. If it is 0, no second tag was found and thus it should not be added into the array.
    Have you actually test that?
    With the second Split(), you always pick the first item (0) so you never have problem even Tag2 is missing. If Tag2 is missing, the full item will be picked.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  11. #11
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: get text between text

    I consider the tag2 missing an error, because the result isn't between two tags if the second tag is missing.


    Edit!
    Not to say that would be a problem in this case when both tags always exist, but I also think it needs to be pointed out just in case someone will use the same code in their own program after reading this thread

  12. #12

    Thread Starter
    Banned
    Join Date
    Feb 2009
    Posts
    15

    Re: get text between text

    Quote Originally Posted by Merri View Post
    Just to point it out, anhn's code will give invalid results if there are more of the first tag than the last tag. It would need to check whether the UBound of the second tag Split is 1. If it is 0, no second tag was found and thus it should not be added into the array.



    Edit!
    Updating for the title parsing:
    Code:
    Private Sub Form_Load()
        Dim strText As String, strData() As String, strRow() As String
        Dim lngA As Long
        ' open data from file
        Open "C:\data.txt" For Input As #1
            strText = Input(LOF(1), #1)
        Close #1
        ' if we get more than none...
        For lngA = 0 To Between(strText, " title=""", "&c", strData) - 1
            strRow = Split(strData(lngA), """ href=""karte.php?d=", 2)
            If LenB(strRow(0)) Then Debug.Print strRow(1), strRow(0)
        Next lngA
    End Sub


    Edit #2!
    anhn's suggestion functionalized and fixed the issue:
    Code:
    Public Function Between(Text As String, Before As String, After As String, Output() As String) As Long
        Dim lngA As Long, strBetween() As String
        Output = Split(Text, Before)
        For lngA = 1 To UBound(Output)
            strBetween = Split(Output(lngA), After, 2)
            If UBound(strBetween) = 1 Then
                Output(Between) = strBetween(0)
                Between = Between + 1
            End If
        Next lngA
        If Between > 0 Then
            ReDim Preserve Output(Between - 1)
        Else
            Output = Split(vbNullString)
        End If
    End Function
    thanks for your code again
    a new problem is that i cant get the results into text1.text
    how can i do this?

    thanks

Posting Permissions

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



Click Here to Expand Forum to Full Width