[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]=20 then final string =3D = [currentlyApprovedWords] else allow 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:

    
  1. [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]=20 then final string =3D = [currentlyApprovedWords] else allow 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:

RE: [WebDNA] IP Address to country (2008) Re:quit command on NT (1997) can WC render sites out? (1997) Changing RAM-cached browser values ... (2000) New command suggestion (was Modifying databasesmanually) (1997) apostrophe in search item (1997) Speed Admin? (Was FlushDatabases) (1997) WebMerchant - MacAuthorize (1999) WebCat2 beta 11 - new prefs ... (1997) WebStar 4.2 issues (2000) Execute Applescript (1997) WebCat for mass emailings (1997) WebCat2: Found Items syntax, etc. (1997) shipcost and Formulas.db (2000) WCf2 and nested tags (1997) Showif, Hideif reverse logic ? (1997) still having shipCost.db Problem (1997) WebCat2b15MacPlugin - [protect] (1997) To use GREP to boldface text (2003) Version f1 status (1997)