Link to home
Start Free TrialLog in
Avatar of intangiblemedia
intangiblemediaFlag for Afghanistan

asked on

Classic ASP Extract Substring between 2 points

Hi,

This is my text string in ASP:
http://www.google.es/#hl=es&xhr=t&q=glass+curtains&cp=4&pf=p&sclient=psy&site=&source=hp&aq=0&aqi=&aql=&oq=glass+&pbx=1&fp=7a027991ed21ee0a

The string can change but defining start is "&q=" and end is the next "&"

I need to put the part between that into a substring - in this case it would be "glass+curtains"

Thanks,

Ben
SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of intangiblemedia

ASKER

Thanks, that is nearly working now!!

Just one issue:
Microsoft VBScript runtime error '800a0005'

Invalid procedure call or argument: 'Mid'

/includes/seo.inc, line 16

this happened when I changed the code see line 2 from a static string to actually request HTTP Server Variables... I think I need to put "" around it, but I cant work it out.... Please help many thanks
<%
   url = Request.ServerVariables("HTTP_REFERER")
   
   startPos = InStr(url, "q=") + 2
   endPos = InStr(startPos+1, url, "&")

   Response.Write "Start: " & startPos & "<br />"
   Response.Write "End: " & endPos & "<br />"

   If endPos < 0 Then
      endPos = Len(url)
   End If

   query = Mid(url, startPos, endPos - startPos)
   Response.Write query
%>

Open in new window

You shouldn't do. Add:
Response.Write url

Open in new window

Immediately after the line:
url = Request.ServerVariables("HTTP_REFERER")

Open in new window

To verify what value it is actually pulling back.
Its kind of hard because i dont have the test asp page indexed in the search engine if that makes sense...

All I know, I have done various tests:
url = "http://www.google.es/#hl=es&xhr=t&q=cortinas+de+cristal&cp=0&pf=p&sclient=psy&site=&source=hp&aq=0&aqi=&aql=&oq=cortinas+d&pbx=1&fp=7a027991ed21ee0a"

url = "http://www.google.es/#sclient=psy&hl=es&q=glass+curtains&aq=f&aqi=&aql=&oq=&pbx=1&fp=7a027991ed21ee0a"

all work fine if its a static string enclosed with commas... but as soon as I do it live
      

url = Request.ServerVariables("HTTP_REFERER")

it doesnt work...

What do you suggest?
I would guess that HTTP_REFERER isn't pulling back the value that you are expecting it to. Or possibly that the "q" parameter is the last one in the query string. Try changing the code to the following and seeing what happens:

As a sanity check try using:
<%
   url = Request.ServerVariables("HTTP_REFERER")
   
   startPos = InStr(url, "q=") + 2
   If startPos > 0 Then

       endPos = InStr(startPos+1, url, "&")

       Response.Write "Start: " & startPos & "<br />"
       Response.Write "End: " & endPos & "<br />"

       If endPos = 0 Then
          endPos = Len(url)
       End If

       query = Mid(url, startPos, endPos - startPos)
       Response.Write query
    Else
        Response.Write "query parameter not found"
    End If
%>

Open in new window

Why not use a RegExp?  It's cleaner and simpler.
<%
url = Request.ServerVariables("HTTP_REFERER")
   
Dim myRegExp
Set myRegExp = New RegExp
myRegExp.IgnoreCase = True
myRegExp.Global = True
myRegExp.Pattern = "&q=(.*?)&"
Set myMatches = myRegExp.Execute(url)

If myMatches.Count >= 1 Then ' Successful match
	For Each myMatch In myMatches
		For I = 1 To myMatch.SubMatches.Count
			resultStr = myMatch.SubMatches(I-1) ' Return the group result
			Response.Write "Match found and contains " & resultStr 
		Next
	Next	
Else
	Response.Write "No Match Found" ' Match attempt failed
End If
%>

Open in new window

Here you go. This will work every time.
<%
RefURL = Request.ServerVariables("HTTP_REFERER")
sPos = InStr(RefURL, "q=") + 2
ePos = InStr(sPos+1, RefURL, "&")
If ePos = 0 Then ePos = Len(RefURL)   
query = Mid(RefURL, sPos, ePos - sPos)
Response.Write query
%>

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Use the Regex I suggested, it's more forgiving.  If there is no "q=" in the refer string it will exit gracefully.
Thank you all - Splitting points as I nearly had a complete solution but worthyking1 gave me what I needed in the end.