[WebDNA] when you need to crop a string to n chars in length, but make sure to NOT cut off in the middle of a word
This WebDNA talk-list message is from 2011
It keeps the original formatting.
numero = 106533
interpreted = N
texte = Hello yee friendly few who would pay attention to this :-)I had to write this today.. and humble as it is (experts undoubtedly =have their own version already), I thought I might as well post it to =the list, for the archives. Someday we'll add it to the webdna store =thingy. This one goes in the "Free & Useful" dept.=--------------------------------------------------------------------------=----------------------------------------------------------------[function name=3Dfn_smartCropperMaxCharsButWholeWords][!]---this function crops a string so that the string does not =exceed max chars.. but we only crop after a WHOLE word!---[/!][!]---pseudo code:iterate each word in [l_proposedString]build up (concat. into) =[currentlyApprovedWords] (start with just the first word)count chars in iterating word, and add =that count to [l_totalCharCountYetThisString]if =[l_totalCharCountYetThisString] > [l_numCharsAllowedInFinalString]=20thenfinal string =3D =[currentlyApprovedWords]elseallow to cont. =concat. (iterating words of [l_proposedString])---[/!][showif =[URL][numCharsAllowedInFinalString][/URL]=3D[URL][raw][numCharsAllowedInFi=nalString][/raw][/URL]][text]numCharsAllowedInFinalString=3D25[/text][/showif]=[text]l_numCharsAllowedInFinalString=3D[numCharsAllowedInFinalString][/tex=t][text]l_proposedString=3D[proposedString][/text][text]l_finalString=3D[/text][text]l_currentlyApprovedWords=3D[/text][text]l_totalCharCountYetThisString=3D0[/text][ListWords Delimiters=3D[URL] =[/URL]&Words=3D[URL][l_proposedString][/URL]][showif [URL][l_finalString][/URL]=3D]=[text]numCharsThisWord=3D[math][countchars][word][/countchars]+1[/math][/t=ext]=[text]l_totalCharCountYetThisString=3D[math][l_totalCharCountYetThisString=]+[numCharsThisWord][/math][/text][if =([l_totalCharCountYetThisString]>[l_numCharsAllowedInFinalString])][then][!]---too many chars; don't =concat this word on to [l_currentlyApprovedWords], and set =[l_finalString] which is flag to effectively break out of =listWords---[/!]=[text]l_finalString=3D[l_currentlyApprovedWords][/text][/then][else][!]---charCount still OK; allow =to concat on this word ---[/!][!]---beware: [delimiter] bug in =ListWords: (still a bug?)=http://dev.webdna.us/TalkListArchive/DetailResults.tpl?command=3Dsearch&db==3Dwebdna-talk.db&eqIDdata=3D43327&max=3D1&words=3Dlistwords%20delimiter%2=0bug---[/!][text]Delim=3D[delimiter][/text]=[text]l_currentlyApprovedWords=3D[l_currentlyApprovedWords][Delim][word][/=text][/else][/if][/showif][/ListWords]=[text]l_finalString=3D[RemoveHTML][l_currentlyApprovedWords][/RemoveHTML][=/text][return][l_finalString][/return][/function][!]---fn_smartCropperMaxCharsButWholeWords---[/!]=--------------------------------------------------------------------------=----------------------------------------------------------------to call the function:[text]rawOrigString=3DYour Text You Want to Crop Goes Here.[/text][fn_smartCropperMaxCharsButWholeWords =proposedString=3D[URL][rawOrigString][/URL]&numCharsAllowedInFinalString=3D=20]- Govinda--------------Old WebDNA talklist archives: (written here, since they seem to no =longer be auto-suffixed to list posts) -http://dev.webdna.us/TalkListArchive/index.tpl?db=3Dwebdna-talk
Associated Messages, from the most recent to the oldest:
|
- [WebDNA] when you need to crop a string to n chars in length, but make sure to NOT cut off in the middle of a word (Govinda 2011)
|
Hello yee friendly few who would pay attention to this :-)I had to write this today.. and humble as it is (experts undoubtedly =have their own version already), I thought I might as well post it to =the list, for the archives. Someday we'll add it to the webdna store =thingy. This one goes in the "Free & Useful" dept.=--------------------------------------------------------------------------=----------------------------------------------------------------[function name=3Dfn_smartCropperMaxCharsButWholeWords]
[!]---this function crops a string so that the string does not =exceed max chars.. but we only crop after a WHOLE word!---[/!]
[!]---pseudo code:iterate each word in [l_proposedString]build up (concat. into) =[currentlyApprovedWords] (start with just the first word)count chars in iterating word, and add =that count to [l_totalCharCountYetThisString]if =[l_totalCharCountYetThisString] > [l_numCharsAllowedInFinalString]=20thenfinal string =3D =[currentlyApprovedWords]elseallow to cont. =concat. (iterating words of [l_proposedString])---[/!][showif =
[url][numCharsAllowedInFinalString][/URL]=3D
[url][raw][numCharsAllowedInFi=nalString][/raw][/URL]]
[text]numCharsAllowedInFinalString=3D25[/text][/showif]=
[text]l_numCharsAllowedInFinalString=3D[numCharsAllowedInFinalString][/tex=t]
[text]l_proposedString=3D[proposedString][/text]
[text]l_finalString=3D[/text]
[text]l_currentlyApprovedWords=3D[/text]
[text]l_totalCharCountYetThisString=3D0[/text][ListWords Delimiters=3D
[url] =[/URL]&Words=3D
[url][l_proposedString][/URL]][showif
[url][l_finalString][/URL]=3D]=
[text]numCharsThisWord=3D
[math][countchars][word][/countchars]+1[/math][/t=ext]=
[text]l_totalCharCountYetThisString=3D
[math][l_totalCharCountYetThisString=]+[numCharsThisWord][/math][/text][if =([l_totalCharCountYetThisString]>[l_numCharsAllowedInFinalString])][then]
[!]---too many chars; don't =concat this word on to [l_currentlyApprovedWords], and set =[l_finalString] which is flag to effectively break out of =listWords---[/!]=
[text]l_finalString=3D[l_currentlyApprovedWords][/text][/then][else]
[!]---charCount still OK; allow =to concat on this word ---[/!]
[!]---beware: [delimiter] bug in =ListWords: (still a bug?)=http://dev.webdna.us/TalkListArchive/DetailResults.tpl?command=3Dsearch&db==3Dwebdna-talk.db&eqIDdata=3D43327&max=3D1&words=3Dlistwords%20delimiter%2=0bug---[/!]
[text]Delim=3D[delimiter][/text]=
[text]l_currentlyApprovedWords=3D[l_currentlyApprovedWords][Delim][word][/=text][/else][/if][/showif][/ListWords]=
[text]l_finalString=3D
[removehtml][l_currentlyApprovedWords][/RemoveHTML][=/text]
[return][l_finalString][/return][/function]
[!]---fn_smartCropperMaxCharsButWholeWords---[/!]=--------------------------------------------------------------------------=----------------------------------------------------------------to call the function:
[text]rawOrigString=3DYour Text You Want to Crop Goes Here.[/text][fn_smartCropperMaxCharsButWholeWords =proposedString=3D
[url][rawOrigString][/URL]&numCharsAllowedInFinalString=3D=20]- Govinda--------------Old WebDNA talklist archives: (written here, since they seem to no =longer be auto-suffixed to list posts) -http://dev.webdna.us/TalkListArchive/index.tpl?db=3Dwebdna-talk
Govinda
DOWNLOAD WEBDNA NOW!
Top Articles:
Talk List
The WebDNA community talk-list is the best place to get some help: several hundred extremely proficient programmers with an excellent knowledge of WebDNA and an excellent spirit will deliver all the tips and tricks you can imagine...
Related Readings:
shared_POP and archiving your own mailing list (2003)
Multiple catalog databases and showcart (1997)
FYI: virus alert (1996)
[WebDNA] To be or not to be friendly (URLs) (2011)
math on date? (1997)
Range compare... (2000)
Only charge card when product shipped ? (1997)
[isfolder] and [filename] (1997)
range searching (1998)
validatin on NT (sorry for the dups and etc) (1998)
[OT] Argh... (2004)
emailer on Windows Beta 18 (1997)
WebCatalog for guestbook ? (1997)
WebCat2 several catalogs? (1997)
Forms Search Questions (1997)
Non-SGML Characters and databases (2004)
writefile and texte (1997)
RedHat Linux glibc version 2.1.2 or higher required (2000)
How reliable is WebDNA? (2004)
5.0 feature request: Sort for ListWords (2001)