| Author |
Topic Search Topic Options
|
neotrix
Mod Builder Group
Joined: 09 November 2003
Location: Pakistan
Status: Offline
Points: 433
|
Post Options
Thanks(0)
Quote Reply
Topic: Extracting Values From A Variable 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" 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
Edited by neotrix
|
|
|
 |
dpyers
Senior Member
Joined: 12 May 2003
Status: Offline
Points: 3937
|
Post Options
Thanks(0)
Quote Reply
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
Edited by dpyers
|
Lead me not into temptation... I know the short cut, follow me.
|
 |
neotrix
Mod Builder Group
Joined: 09 November 2003
Location: Pakistan
Status: Offline
Points: 433
|
Post Options
Thanks(0)
Quote Reply
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" 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 ?
|
|
|
 |
dpyers
Senior Member
Joined: 12 May 2003
Status: Offline
Points: 3937
|
Post Options
Thanks(0)
Quote Reply
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
Edited by dpyers
|
Lead me not into temptation... I know the short cut, follow me.
|
 |
neotrix
Mod Builder Group
Joined: 09 November 2003
Location: Pakistan
Status: Offline
Points: 433
|
Post Options
Thanks(0)
Quote Reply
Posted: 19 June 2004 at 11:37pm |
Thankyou Very Much Sir,
By the way www.new2asp.com is Kewl!!! 
Let me try what you wrote
|
|
|
 |
neotrix
Mod Builder Group
Joined: 09 November 2003
Location: Pakistan
Status: Offline
Points: 433
|
Post Options
Thanks(0)
Quote Reply
Posted: 19 June 2004 at 11:39pm |
WOW! the example page you gave me worked 
Thankyou Very Much
|
|
|
 |