[lp_string_firstwords]

Description

Link: [lp_string_firstwords]
Author: Bil Corry
Category: String
Version: 8.x
License: Public Domain
Posted: Dec. 03, 2005
Updated: Mar. 19, 2008
More by this author...

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[

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

none

Email:


Password:



Newest

Most Popular