[string_truncate2]

Description

Link: [string_truncate2]
Author: Jason Huck
Category: String
Version: 8.5.x
License: Public Domain
Posted: Jan. 04, 2008
Updated: Jan. 01, 0001
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.

Parameters

-text string, required The source text to truncate.
-length integer, required The number of characters that should be in the returned string.

Sample Usage

var('str') = 'The quick, brown-fox jumps over the "lazy" dog.'; 
 
loop($str->size); 
    loop_count + ' - ' + string_truncate($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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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;

 

Related Tags



Comments

none

Email:


Password:



Newest

Most Popular

Support tagSwap.net