Lasso Soft Inc. > Home

[lp_string_firstwords]

Linklp_string_firstwords
AuthorBil Corry
CategoryString
Version8.x
LicensePublic Domain
Posted03 Dec 2005
Updated19 Mar 2008
More by this author...

Description

Returns the first series of words in a string, up to a max length.

Sample Usage

// test vector adapted from Jason Huck: http://devblog.jasonhuck.com/2007/10/02/smart-string-truncation-in-lasso/

'
';
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;
'
';

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;

]

Related Tags

Comments

11 Apr 2011, Steve Piercy

Workaround for PCRE issues

In Lasso 8.x, the string_ regular expression tags use PCRE library which is buggy, whereas the regexp type uses the ICU library. More information:
http://lasso.2283332.n4.nabble.com/Lassowiki-not-doing-well-on-Centos-or-something-tp3146844p3146855.html

The following is a workaround replacement for this tag.

[
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('r') = regexp(-find=('(?is)(^.{1,'+#max_length+'})'+#delimiter), -input=#string);
local('result') = #r->split;
if(#result->size >= 2);
return(#result->get(2));
/if;
return(''); // empty string when no words are found
/define_tag;
]

Please log in to comment

LassoSoft Inc. > Home

 

 

©LassoSoft Inc 2015 | Web Development by Treefrog Inc | PrivacyLegal terms and Shipping | Contact LassoSoft