[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:

WebCatalog can't find database (1997) WebCat2b15MacPlugin - showing [math] (1997) RE: Quantity (1998) sendmail spaces (1997) database size? (1997) [WebDNA] [ipaddress] - [RealIP] - Not working properly (2016) Updating a database once per day - An example (1998) Modtime bug? (2002) Two submit buttons ? (1997) Purchased cart being overwritten - still !?? (1997) WebCatalog Technical Reference (1997) Shipping Calculation Problem (1997) WebCatalog for guestbook ? (1997) WebCat2 - [format thousands] (1997) More on the email templates (1997) Can't get appendfile to work (1997) formatting dates from a field ... (1997) Template Encrypt Speed (1998) Banners and sort of random display (1997) [OT] Happy Turkey Day! (2003)