[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:
Mozilla/4. and Browser Info.txt (1997)
webcat2b12 CGI -- Date comparisons (1997)
Sort Order on a page search (1997)
Protect (1997)
select multiple 2 more cents (1997)
[OT] Adopting out a WebDNA website (2003)
Strange error (2000)
unable to launch acgi in WebCat (1997)
Size limit for tmpl editor ? (1997)
Running subtotal? (1998)
FAX orders (1996)
TeaRoom Order fields email account remain empty even though thewy are filled. (1997)
sendmail for email (was Netforms) (1998)
OT: Unknown Webstar Error ruining presentation?! (2001)
randomness (1997)
Almost a there but..bye bye NetCloak (1997)
[sendmail] and [formvariables] (1997)
I give up!! (1997)
Multiple fields on 1 input (1997)
Help! WebCat2 bug (1997)