[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:
For those of you not on the WebCatalog Beta... (1997)
Emailer setup (1997)
Grep Help (2007)
Using the sendmail command on CGate Pro (Unix) (2000)
SET (1998)
Location of Browser Info.txt file (1997)
(1998)
RequiredFields template (1997)
WebCat2b12 - New features for 1.6 users ... (1997)
Round up prices (2000)
WebTen? (1997)
one item in several different categories? (2000)
Max Record length restated as maybe bug (1997)
formula inside database to calculate weights (1997)
PSC recommends what date format yr 2000??? (1997)
problems with WebCat-Plugin ()
[LOOKUP] (1997)
Aaron kant add (or whatever it was) (2000)
Error Lob.db records error message not name (1997)
form data submission gets truncated (1997)