Gossamer Forum
Home : Products : Gossamer Links : Development, Plugins and Globals :

global to cut short variable

Quote Reply
global to cut short variable
i need a global that will cut short any variable up to a certain amount of characters like

<%left($description,50)%>

would give me: "my desript..."

i just have no idea about how to go about doing it
Quote Reply
Re: [kzap] global to cut short variable In reply to
Something like this:

sub {
my $field = shift;
my $length=shift;
return substr($field, 0, $length) . '...';

}
Quote Reply
Re: [afinlr] global to cut short variable In reply to
Thanks =)
Quote Reply
Re: [kzap] global to cut short variable In reply to
Or, if you don't really want the words to get cut off midway;

Code:
sub {
my $field = $_[0];
my $limit = $_[1];
my @sliced = split(" ",$field);

my ($count,$back);
foreach(@sliced) {
if ($count < $limit) {
$back .= $_ . " ";
} else {
$back .= "...";
if ($count == $limit) { last; }
}
$count++;
}

return $back;

}

<%global_name($Description,NumberOfWords)%>

Its a bit more long winded.. but it should do what you want.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] global to cut short variable In reply to
hmm cool but how about a combination of both

lets say it counts the number of words for every loop it keeps checking the number of characters in $back and once its greater than the specified value $limit, it goes back one word and adds "..." ?
Quote Reply
Re: [kzap] global to cut short variable In reply to
No idea I'm afraid. Don't have much free time at the moment, otherwise I would have a play.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [kzap] global to cut short variable In reply to
Something like this I think. If it ends in a space it should just cut off the space but if it is a space and then letters it will assume this is an incomplete word and cut this off as well.

sub {
my $field = shift;
my $length=shift;
my $short = substr($field, 0, $length) ; $short =~ s/\s\S*$//; $short .= '...';

return $short;

}