Link | string_truncate2 |
Author | Jason Huck |
Category | String |
Version | 8.5.x |
License | Public Domain |
Posted | 04 Jan 2008 |
Updated | 04 Jan 2008 |
More by this author... |
This tag accepts a string and a number of characters, and returns the string truncated to that length. It makes allowances for whitespace and punctuation, and appends an ellipsis character to the end. A complete description can be found here.
var('str') = 'The quick, brown-fox jumps over the "lazy" dog.'; loop($str->size); loop_count + ' - ' + string_truncate($str, loop_count) + '\n'; /loop;
Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.
define_tag( 'truncate', -namespace='string_', -req='text', -req='length', -type='integer', -copy, -priority='replace', -encodenone, -description='Truncates the given string to the given number of characters.' ); // if the original string is shorter than or equal to the desired length, // just return it unaltered. #text->size <= #length ? return(#text); local('out') = string; // while #out is empty, #length is still greater than zero, // and the last character of the new string is not whitespace... while(!#out->size || !#out->iswhitespace(#out->size) && #length); // store a new substring in #out #out = #text->substring(1, #length); // decrement #length by 1 #length -= 1; /while; // if we reached zero, return nothing !#length ? return; // remove any trailing non-alphanumeric characters and whitespace #out = string_replaceregexp( #out, -find='[^A-Za-z0-9]*\\s*$', -replace='' ); // return the final result with an ellipsis character appended return(#out + '…'); /define_tag;
No comments
©LassoSoft Inc 2015 | Web Development by Treefrog Inc | Privacy | Legal terms and Shipping | Contact LassoSoft