Solved

Classic ASP Extract Substring between 2 points

Posted on 2011-03-17
10
1,875 Views
Last Modified: 2012-05-11
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
Comment
Question by:intangiblemedia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 200 total points
ID: 35155166
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
 

Author Comment

by:intangiblemedia
ID: 35155343
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35155367
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:intangiblemedia
ID: 35155449
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35155478
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
 
LVL 18

Expert Comment

by:mgfranz
ID: 35158375
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
 
LVL 6

Expert Comment

by:worthyking1
ID: 35209454
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
 
LVL 6

Accepted Solution

by:
worthyking1 earned 300 total points
ID: 35209486
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
 
LVL 18

Expert Comment

by:mgfranz
ID: 35209623
Use the Regex I suggested, it's more forgiving.  If there is no "q=" in the refer string it will exit gracefully.
0
 

Author Closing Comment

by:intangiblemedia
ID: 35216463
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question