Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


Tutorial GetBetween Function [Grab text inbetween 2 HTML elements] filter_list
Author
Message
GetBetween Function [Grab text inbetween 2 HTML elements] #1
Function:
Code:
Private Function GetBetween(ByVal Start As Long, ByVal Data As String, _
    ByVal StartString As String, ByVal EndString As String, _
    Optional ByVal CompareMethod As CompareMethod = vbBinaryCompare) As String

  Dim lonStart As Long, lonEnd As Long

  '1. Find start string.
  lonStart = InStr(Start, Data, StartString, CompareMethod)

  If lonStart > 0 Then
    '2. Move to end of start string.
    lonStart = lonStart + Len(StartString)

    '3. Find end string.
    lonEnd = InStr(lonStart, Data, EndString, CompareMethod)

    If lonEnd > 0 Then
    '4. Extract data between start and end strings.
    GetBetween = Mid$(Data, lonStart, lonEnd - lonStart)
    End If
  End If

    End Function
Usage:

Code:
GetBetween "IndexToStart, WhereToGet, FrontOfString,BackOfString"

Example:

Let's say you had a variable declared as "convo", and the data stored inside that variable was "Goon sucks at AF tutorials.", now the syntax for it all would be;

Code:
Dim Convo As String = "Goon sucks at AF Tutorials"
Now let's grab the "AF" out of the sentence.

Completed:
Code:
Dim Convo As String = "Goon sucks at AF Tutorials"
Dim Forums As String = GetBetween(1,Convo,"sucks at","Tutorials")

MsgBox ("This tutorial is written on "" & Forums & "")
Which would pop up and say, "This tutorial is written on AF".
##
Example #2) WebBased


Hope I explained it clearly enoug, sorry if I didn't. It's my 1st "ever" tutorial

Reply

RE: GetBetween Function [Grab text inbetween 2 HTML elements] #2
Thanks man, this is very useful!
I'd do it like this though:
Code:
dim x, y, z as string
x = "Hello, my name is SQLi. I work for the government."
y = x.split("is ")(1)
z = y.split(". I work")(0)

Reply

RE: GetBetween Function [Grab text inbetween 2 HTML elements] #3
The function is using old and slow VB6 carry over methods.
-- cxS

[ Haskell/.NET/C/C++ - Software Engineer ]

Reply







Users browsing this thread: 1 Guest(s)