Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - Extracting Values From A Variable
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Extracting Values From A Variable

 Post Reply Post Reply
Author
neotrix View Drop Down
Mod Builder Group
Mod Builder Group
Avatar

Joined: 09 November 2003
Location: Pakistan
Status: Offline
Points: 433
Post Options Post Options   Thanks (0) Thanks(0)   Quote neotrix Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
dpyers View Drop Down
Senior Member
Senior Member


Joined: 12 May 2003
Status: Offline
Points: 3937
Post Options Post Options   Thanks (0) Thanks(0)   Quote dpyers Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
neotrix View Drop Down
Mod Builder Group
Mod Builder Group
Avatar

Joined: 09 November 2003
Location: Pakistan
Status: Offline
Points: 433
Post Options Post Options   Thanks (0) Thanks(0)   Quote neotrix Quote  Post ReplyReply Direct Link To This Post 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 ?

Back to Top
dpyers View Drop Down
Senior Member
Senior Member


Joined: 12 May 2003
Status: Offline
Points: 3937
Post Options Post Options   Thanks (0) Thanks(0)   Quote dpyers Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
neotrix View Drop Down
Mod Builder Group
Mod Builder Group
Avatar

Joined: 09 November 2003
Location: Pakistan
Status: Offline
Points: 433
Post Options Post Options   Thanks (0) Thanks(0)   Quote neotrix Quote  Post ReplyReply Direct Link To This Post 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

Back to Top
neotrix View Drop Down
Mod Builder Group
Mod Builder Group
Avatar

Joined: 09 November 2003
Location: Pakistan
Status: Offline
Points: 433
Post Options Post Options   Thanks (0) Thanks(0)   Quote neotrix Quote  Post ReplyReply Direct Link To This Post Posted: 19 June 2004 at 11:39pm

WOW! the example page you gave me worked

Thankyou Very Much

Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.