Extracting Values From A Variable
Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Classic ASP Discussion
Forum Description: Discussion on Active Server Pages (Classic ASP).
URL: https://forums.webwiz.net/forum_posts.asp?TID=10904
Printed Date: 31 March 2026 at 1:17pm Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com
Topic: Extracting Values From A Variable
Posted By: neotrix
Subject: Extracting Values From A Variable
Date Posted: 17 June 2004 at 11:05am
|
Hello, this code i use to Read a page, save it in a variable, and then extract certain part of that variable (like everything between [mycode][/mycode]
<%
Sub LoadThePage(strPageText, strInputURL) Set objXMLHTTP = Server.CreateObject("Microsoft.XMLHTTP") objXMLHTTP.Open "GET", strInputURL, False objXMLHTTP.Send strPageText = objXMLHTTP.responseText Set objXMLHTTP = Nothing
End Sub
Sub PullStrings(strSearchString, strEndStringChars)
'This entire sub routine could be on two lines, but for illustration purposes it is broken up
Dim strStartPos Dim strEndPos Dim strLength strStartPos = 0 strEndPos = 0 strLength = 0 strStartPos = instr(strPageText,strSearchString) strEndPos = instr(strStartPos, strPageText, strEndStringChars) strLength = strEndPos - strStartPos
'Pluck the string out of the page text response.write mid(strPageText,strStartPos,strLength) & "<br/>"
End Sub
'************* Mainline Code ****************
Dim strPageText Dim strSearchString Dim strEndStringChars Dim strInputURL strInputURL = " http://neotrix/pages/page.htm - http://neotrix/pages/page.htm http://neotrix/testing/ - " LoadThePage strPageText, strInputURL
for i = 1 to 20 PullStrings "[mycode]", "[/mycode]" next
'************* END Mainline Code ************
%>
[/quote]
Now the problem i'm facing, is that it only matches for the first [mycode] and [/mycode] in the page that i have read, how ever there are 20 areas in that page of such text, but this matches for the first line only, how can i make it extract all the parts of page that begin and end with [code] and |
Sorry if i cannot explain my self, but please help
------------- http://www.muhammadbinyusrat.com/blog/" rel="nofollow - Say to the believing men..
|
Replies:
Posted By: dpyers
Date Posted: 17 June 2004 at 1:07pm
|
The problem is that the Pullstrings sub routine starts searching at position 0 each tme it's called. You need to take the initialization of the start and end positions outside of the sub routine, and then track/adjust the start and end positions within the subroutine.
Move these lines to just before the For-Next Loop
Dim strStartPos Dim strEndPos Dim strLength strStartPos = 0 strEndPos = 0
|
And then change these lines
strStartPos = instr(strPageText,strSearchString) strEndPos = instr(strStartPos, strPageText, strEndStringChars) |
To This
strStartPos = instr(strStartPos, strPageText,strSearchString) strEndPos = instr(strStartPos + 1, strPageText, strEndStringChars)
|
After the response.write, insert this line
strStartPos = strEndPos + 1 |
NOT TESTED
-------------
Lead me not into temptation... I know the short cut, follow me.
|
Posted By: neotrix
Date Posted: 19 June 2004 at 2:06am
|
this is the final code:
<%
Sub LoadThePage(strPageText, strInputURL) Set objXMLHTTP = Server.CreateObject("Microsoft.XMLHTTP") objXMLHTTP.Open "GET", strInputURL, False objXMLHTTP.Send strPageText = objXMLHTTP.responseText Set objXMLHTTP = Nothing
End Sub
Sub PullStrings(strSearchString, strEndStringChars)
'This entire sub routine could be on two lines, but for illustration purposes it is broken up
strLength = 0 strStartPos = instr(strStartPos, strPageText,strSearchString) strEndPos = instr(strStartPos, strPageText, strEndStringChars)
strLength = strEndPos - strStartPos
'Pluck the string out of the page text response.write mid(strPageText,strStartPos,strLength) & "<br/>" strStartPos = strEndPos + 1
End Sub
'************* Mainline Code ****************
Dim strPageText Dim strSearchString Dim strEndStringChars Dim strInputURL strInputURL = " http://neotrix/pages/page.htm - http://neotrix/pages/page.htm " LoadThePage strPageText, strInputURL
Dim strStartPos Dim strEndPos Dim strLength strStartPos = 0 strEndPos = 0
for i = 1 to 20 PullStrings "[mycode]", "[/mycode]" next
'************* END Mainline Code ************
%>
|
and this is the final output
Microsoft VBScript runtime error '800a0005'
Invalid procedure call or argument: 'instr'
/testing/pakmuziq.asp, line 19
|
 I See no Clue ?
------------- http://www.muhammadbinyusrat.com/blog/" rel="nofollow - Say to the believing men..
|
Posted By: dpyers
Date Posted: 19 June 2004 at 1:52pm
|
The string you're searching for is not there. Do a View Source on your input file.
Also, the line
strEndPos = instr(strStartPos, strPageText, strEndStringChars) |
Should be
strEndPos = instr(strStartPos + 1, strPageText, strEndStringChars) |
Adding the 1 will prevent you from getting the same result returned if the start and end strings are the same thing.
Also, that code will give you data from the beginning of the Start string to the beginning of the End string. If you just want the data between the strings, replace this line
response.write mid(strPageText,strStartPos ,strLength) & "<br/>" & |
With this line
response.write mid(strPageText,strStartPos + len(strSearchString),strLength - len(strSearchString) ) & "<br/>" & |
Because this code is getting a little over modified, I set up an example page at http://www.new2asp.com/stuff/PullStrings/PullStrings.asp - http://www.new2asp.com/stuff/PullStrings/PullStrings.asp
-------------
Lead me not into temptation... I know the short cut, follow me.
|
Posted By: neotrix
Date Posted: 19 June 2004 at 11:37pm
|
Thankyou Very Much Sir,
By the way http://www.new2asp.com - www.new2asp.com is Kewl!!! 
Let me try what you wrote
------------- http://www.muhammadbinyusrat.com/blog/" rel="nofollow - Say to the believing men..
|
Posted By: neotrix
Date Posted: 19 June 2004 at 11:39pm
|
WOW! the example page you gave me worked 
Thankyou Very Much
------------- http://www.muhammadbinyusrat.com/blog/" rel="nofollow - Say to the believing men..
|
|