Solved

Classic ASP Extract Substring between 2 points

Posted on 2011-03-17
10
1,827 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
  • 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Autosum input type=text when checkbox is true 28 82
add custom headers to my mail 2 47
Calculating value of records from a column - classic asp 7 67
Pass through dll 2 62
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

947 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now