Forum Moderators: open

Message Too Old, No Replies

"Grabbing" text from between two comment tags with VBScript

Can it be done - and if so, how?!?

         

HyperGeek

6:20 pm on Jan 20, 2004 (gmt 0)

10+ Year Member




Let's say that I have an HTML page laid out like this:

<!--// START //-->
Grab this text.
<!--// FINISH //-->

How would I grab that text and output it seperate from the rest of the page?

RossWal

6:38 pm on Jan 20, 2004 (gmt 0)

10+ Year Member



Server-side or client-side?

Server-side: Look into reading the file with the filesystem object & using instr() to parse it.

Client-side: Consider using a div or span tag instead of the html comments as delimeters. Then you can access the innerhtml property (I think it's innerhtml search google to find it).

HTH,
Ross

f00sion

12:32 am on Jan 21, 2004 (gmt 0)

10+ Year Member



if its local, open the file using the file systems object, if its remote, use the msxml serverhttp object to grab the file... either way you would then use regular expressions to parse out the text that you want.

HyperGeek

7:15 pm on Jan 21, 2004 (gmt 0)

10+ Year Member



As an experiment, I'm basically building a spider for my own site. I access the page and everything, but I don't have the slightest idea how to grab the text between - let's say:

<TITLE> <-- Here and Here --> </TITLE>

I'm self-taught in VBScript, so I really don't know where to start with this one. I know it has something to do with InStr, but have no idea how to apply it.

Care to lend a hand?

f00sion

9:28 pm on Jan 21, 2004 (gmt 0)

10+ Year Member



here ya go:

foo = "<html><title>some crazy title</title>blah blah blah<br>asdf</html>" 'your html text
Set re = new RegExp 'new regular expressions object
re.IgnoreCase = true 'ignore case when matching
re.Global = false 'stop after first match
re.Pattern = "<title>(.*)?</title>" 'pattern to match
set matches = re.Execute(foo) 'match with contents of foo variable
Response.Write matches.item(0).submatches(0) 'contents of title tag

too much information

9:45 pm on Jan 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Or you could just split the string a few times:

parse_string1 = foo 'see above post
parse_string1 = replace(parse_string1,"<title>","¦")
parse_string = split(parse_string1,"¦")
parse = parse_string(1)
parse_string1 = replace(parse,"</title>","¦")
parse_string = split(parse_string1,"¦")
parsed_string = parse_string(0)

Response.write parsed_string 'gives final result

:)

HyperGeek

5:35 pm on Jan 23, 2004 (gmt 0)

10+ Year Member



Awesome, works really well - however - when it cannot find the tags I specified, I get this error:

Microsoft VBScript runtime error '800a01c2'
Wrong number of arguments or invalid property assignment
/test/script.asp, line 22

I tried to place a conditional statement in there like:

IF Len(Trim(var)) <= 0 THEN
Response.Write "No content found."
ELSE
Response.Write matches.item(0).submatches(0)
END IF

But it didn't work - I assume because it's not even getting to the point of where it can run the condition.

Any suggestions?

Thanks for all of your help.

too much information

7:08 pm on Jan 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Set Matches = objRead.Execute(text)
If Matches.Count > 0 Then
For Each Match In Matches
(Do your stuff here)
Next
End If

That should fix you up! (I've been reading on this too. The parsing thing just can't cut it for a complicated search) ;)