Solved

Classic ASP Extract Substring between 2 points

Posted on 2011-03-17
10
1,819 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 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

13 Experts available now in Live!

Get 1:1 Help Now