Returns the first series of words in a string, up to a max length.
Parameters
-string
string, required
The string to pull the first words from.
-max_length
integer, required
The maximum number of characters the returned string can be.
-delimiter
string, optional
Optionally can pass in a regex string to delimit the words. Default is any non-alphanumeric character.
Sample Usage
// test vector adapted from Jason Huck: http://devblog.jasonhuck.com/2007/10/02/smart-string-truncation-in-lasso/
'<pre>';
var('str' = 'The quick, brown-fox jumps over it\'s "lazy" dog.');
loop($str->size + 10);
loop_count + ' - ' + lp_string_firstwords($str, loop_count) + '\n';
/loop;
'</pre>';
Source Code
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('lp_string_firstwords',
-description='Returns the first series of words in a string, up to a max length.',
-priority='replace',
-required='string',
-required='max_length',
-optional='delimiter'); // default is any non-alphanumeric character
!local_defined('delimiter') ? local('delimiter' = '(?:$|\\W)+');
local('result' = string_findregexp(#string, -find='(?is)(^.{1,'+#max_length+'})'+#delimiter));
if(#result->size == 2);
return(#result->get(2));
/if;
return(''); // empty string when no words are found
/define_tag;
]