Gossamer Forum
Home : General : Perl Programming :

Email Functions

Quote Reply
Email Functions
I was wondering whether or not there are any good Regular Expressions for transforming nonwrapped text into lines of about 70 characters long (with exceptions of course where lines are broken between words not inside a word itself if a word cannot fit on a line).

In particular, this is for an e-mail program I'm cooking up. Thanks for any help you can provide.

------------------
- Bryant Luk
http://www.swgamers.com/
Quote Reply
Re: Email Functions In reply to
I found:

Code:
sub linewrap {
# --------------------------------------------------------
# Wraps a line into 60 char chunks. Modified from code by
# Tim Gim Yee <tgy@chocobo.org>.
#
my $line = shift; defined $line or return '';
my @data = split /\t/, $line;
my $columns = 60;
my $tabstop = 1;
my $frag = '';
my $col = $columns - 1;

for (@data) {
$_ = "$frag$_";
$frag = '';
s/(.{1,$columns}$)|(.{1,$col}(?:\S\s+|-(?=\w)))|(.{$col})/
$3 ? "$3-\n" :
$2 ? "$2\n" :
(($frag = $1), '')
/ge;
$frag .= (' ' x ($tabstop - length($frag) % $tabstop));
}

local $_ = join '', @data, $frag;
s/\s+$//gm;
return $_;
}

on the net..

Hope that helps,

Alex