How do I do this with VB?

T

the other john

I'm trying to extract the first 3 words (or 4 or whatever) from a
recordset field so I can format it with CSS. I'm not sure how to do
this. mid() won't work because there's no way to know how many
characters I'm dealing with.

In short I what to dynamically retrieve the first few words from a
field, format it with css, deliver it, then continue where it left off
without the css.

Any suggestions?

Thanks!
 
M

Mike Brind

the said:
I'm trying to extract the first 3 words (or 4 or whatever) from a
recordset field so I can format it with CSS. I'm not sure how to do
this. mid() won't work because there's no way to know how many
characters I'm dealing with.

In short I what to dynamically retrieve the first few words from a
field, format it with css, deliver it, then continue where it left off
without the css.

Do you mean VBScript? Hope so...

<%
Dim str, i, spacepos, word
Response.Write "<strong>"
str = "My first three words in the sentence are bold"
for i = 1 to 3
spacepos = instr(str," ")
word = left(str,spacepos)
str = right(str,len(str)-spacepos)
response.write word
next
Response.Write "</strong>"
Response.Write str
%>
 
T

the other john

Yes, VBScript.

Assuming that there's no single function then to do this in ASP?

Thanks Mike, Cool!
 
B

Bob Barrows [MVP]

You could use a regular expression. Somebody else will need to provide it if
you can't come up with one via google.
 
A

Anthony Jones

Bob Barrows said:
You could use a regular expression. Somebody else will need to provide it if
you can't come up with one via google.

It's a bit tricky. This is the best I've come up with:-


Dim rgx: set rgx = new RegExp

rgx.Pattern = "((?:\b\w+\b.*?){3})(.*)?"

sHTML = rgx.Replace(rst("MyField").value, "<strong>$1</strong>$2"

It works when there are 3 or more words but fails when there are 0,1 or 2
words in the string.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,037
Latest member
MozzGuardBugs

Latest Threads

Top