Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1947
  • Last Modified:

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
0
intangiblemedia
Asked:
intangiblemedia
  • 3
  • 3
  • 2
  • +1
2 Solutions
 
Carl TawnSystems and Integration DeveloperCommented:
Try this:
<%
   url = "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"
   
   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

0
 
intangiblemediaAuthor Commented:
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

0
 
Carl TawnSystems and Integration DeveloperCommented:
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.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
intangiblemediaAuthor Commented:
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?
0
 
Carl TawnSystems and Integration DeveloperCommented:
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

0
 
mgfranzCommented:
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

0
 
worthyking1Commented:
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

0
 
worthyking1Commented:
Actually, let's alter that to account for a page visit that does NOT come from Google (otherwise it would generate an error if it could not find the "q=" starting point).
<%
RefURL = Request.ServerVariables("HTTP_REFERER")
sPos = InStr(RefURL, "q=") + 2
If sPos > 2 then
	ePos = InStr(sPos+1, RefURL, "&")
	If ePos = 0 Then ePos = Len(RefURL)   
	query = Mid(RefURL, sPos, ePos - sPos)
End if
Response.Write query
%>

Open in new window

0
 
mgfranzCommented:
Use the Regex I suggested, it's more forgiving.  If there is no "q=" in the refer string it will exit gracefully.
0
 
intangiblemediaAuthor Commented:
Thank you all - Splitting points as I nearly had a complete solution but worthyking1 gave me what I needed in the end.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now