Click to See Complete Forum and Search --> : stop showing text after 60 characters...
Hi There.
I have a lot of text in my database and I want to show the user only the first 60 characters of the text. After that He'll have a More Info link that will give him the rest.
How can I do the 60 characters thing?
Thanks!
javaNoobie
10-14-2004, 09:01 PM
Use Left()
str1 = Left(str1,60)
Thanks!
How can I do the same but with counting words?
javaNoobie
10-14-2004, 09:17 PM
You mean get the first 60 words? Or get the first 60 characters but display words only?
How can I get the first 20 words?
javaNoobie
10-14-2004, 09:31 PM
Using a while loop, loop thru the whole string and look for spaces. When a space is found, increment a counter. If the counter <20, return the whole string. If counter >=20, return the string up to the 20th spacing found.
or something like it by any chance?
javaNoobie
10-14-2004, 09:39 PM
Wrote a quick one, I have yet to test this. Hopefully it works.
Dim spacePos, count, posToEnd
spacePos = InStr(str1," ")
count = 0
posToEnd = Len(str1)
While spacePos > 0
count = count + 1
spacePos = InStr(spacePos+1, str1, " ")
if count >= 20 then
posToEnd = spacePos
exit while
end if
wend
str1 = Left(str1, posToEnd)
EDIT: forgot to add one to starting find position in the loop..
some problem in the while I think...
schizo
10-25-2004, 05:53 PM
<%
Function getWords(input_str, words_int)
Dim wordCount_int, output_str
Do While wordCount_int < words_int
Dim spaceIndex_int
spaceIndex_int = InStr(input_str, " ")
output_str = output_str & Left(input_str, spaceIndex_int)
input_str = Right(input_str, (Len(input_str) - spaceIndex_int))
wordCount_int = wordCount_int + 1
Loop
getWords = output_str
End Function
%>
do I define after how many words it'll stop?
schizo
10-26-2004, 01:21 PM
Define it wherever you want and pass it through the function. The second parameter is how many words you want.
Dim twentyWords
twentyWords = getWords(rs("text"), 20)
here's the code:
Function getWords(rs("text"), 20)
Dim wordCount_int, output_str
Do While wordCount_int < words_int
Dim spaceIndex_int
spaceIndex_int = InStr(input_str, " ")
output_str = output_str & Left(input_str, spaceIndex_int)
input_str = Right(input_str, (Len(input_str) - spaceIndex_int))
wordCount_int = wordCount_int + 1
Loop
getWords = output_str
End Function
<%=output_str%>
I'm getting this error:
Microsoft VBScript compilation error '800a03ea'
Syntax error
/reeve/products.asp, line 59
Function getWords(rs("description"), 20)
Why is that?
schizo
10-26-2004, 01:43 PM
No no no... there's no need to modify the function I wrote, just call it like below:
<%
Function getWords(input_str, words_int)
Dim wordCount_int, output_str
Do While wordCount_int < words_int
Dim spaceIndex_int
spaceIndex_int = InStr(input_str, " ")
output_str = output_str & Left(input_str, spaceIndex_int)
input_str = Right(input_str, (Len(input_str) - spaceIndex_int))
wordCount_int = wordCount_int + 1
Loop
getWords = output_str
End Function
'ENTER YOUR DATABASE QUERY HERE
Dim twentyWords
twentyWords = getWords(rs("text"), 20)
Response.Write(twentyWords)
%>
but there's no text showing up at all.
schizo
10-26-2004, 03:24 PM
Does your record set from your query print out okay?
schizo
10-26-2004, 03:50 PM
Seems to work fine for me, I'd say check your record set again.
<%
Function getWords(input_str, words_int)
Dim wordCount_int, output_str
Do While wordCount_int < words_int
Dim spaceIndex_int
spaceIndex_int = InStr(input_str, " ")
output_str = output_str & Left(input_str, spaceIndex_int)
input_str = Right(input_str, (Len(input_str) - spaceIndex_int))
wordCount_int = wordCount_int + 1
Loop
getWords = output_str
End Function
Dim input, twentyWords
input = "word1 word2 word3 word4 word5 word6 word7 word8 word9 word10 word11 word12 word13 word14 word15 word16 word17 word18 word19 word20 word21 word22 word23 word24 word25"
twentyWords = getWords(input, 20)
Response.Write(twentyWords)
%>
'Outputs
word1 word2 word3 word4 word5 word6 word7 word8 word9 word10 word11 word12 word13 word14 word15 word16 word17 word18 word19 word20