****************************** '************************************************** '* Script written by: Luke Latimer '* on: 18/09/2000 '* '* Name: ParseText.vbs '* Description: '* This script consists of a function (ParseText) that '* takes as parameters a string, and a number, and a '* Line Break delimiter (can be VbCrlf). '* The output is the text containing the delimiter according to the '* value passed. If there are spaces present in the text '* The function will attempt to avoid splitting words. '* Use arr=Split(ParsedText,Delimiter) to extract the lines from the output. '* '****************************************************
Dim Text, WrapAt, Delimiter WrapAt=80 Delimiter="#Delimiter#"
'For use when testing with WSH 'text=inputbox("Enter some text, if it's bigger than " & WrapAt & " chars then it will be wrapped onto the next line")
myText=Request.Form("TheTextBox_or_Area") 'Or load from DB
myTextArr=Split(ParseText(myText, WrapAt, Delimiter), Delimiter)
For i=0 to UBound(myTextArr) Response.Write myTextArr(i) & "<BR>" Next
FUNCTION ParseText(Text,WrapAt,Delimiter)
If len(text)>WrapAt then 'start wrapping it
Dim index index=0
Do While Len(Text)>WrapAt 'don't wrap the text halfway through a word if it can be helped
If Instr(Left(Text,WrapAt)," ")=0 Then 'No spaces, split words?
If ParseText="" Then ParseText= Left(Text,WrapAt) Else ParseText=ParseText & Delimiter & Left(Text,WrapAt) End if
If Len(Text)> WrapAt Then Text=Right(Text,Len(Text)-WrapAt) If Len(Text)<=WrapAt Then If ParseText="" Then ParseText= Text Else ParseText=ParseText & Delimiter & Text End if End if End if
Else
tempText=Left(Text,WrapAt) SplitAt=Instr(strReverse(tempText)," ") If SplitAt>0 Then Buffer=1 If SplitAt=WrapAt Then If ParseText="" Then ParseText=TempText Else ParseText=ParseText & Delimiter & TempText End if Else If ParseText="" Then ParseText=Left(tempText,Len(tempText) -SplitAt+Buffer) Else ParseText=ParseText & Delimiter & Left(tempText,Len(tempText) -SplitAt+Buffer) End if End if
If Len(Text)>WrapAt Then If SplitAt=WrapAt Then Text=Right(Text,Len(Text)-WrapAt) Else Text=Right(Text,Len(Text)-(WrapAt-SplitAt+Buffer)) End if If Len(Text)<=WrapAt Then If ParseText="" Then ParseText=Text Else ParseText=ParseText & Delimiter & Text End if End if End if End if Index=Index+1 Loop Else
ParseText=Text
End if
End FUNCTION ****************************** |
|