我试图在 300 个字符处打断,效果很好,但我不希望它在单词中间打断。我想找到一个词的结尾或下一个可用的空格。任何帮助将不胜感激。
foreach (@$comp) {
my $c = $_;
$c->{reviews} = ($c->{reviews} - 1);
my $stars;
if($c->{reviews} == 0) {
$stars = 0;
} else {
$stars = int($c->{reviews_total} / $c->{reviews});
}
$c->{stars} = $stars;
if($c->{title}) {
$c->{name} = $c->{title};
$c->{event} = "FOO";
}
$c->{description} =~ s/BREAK//g;
my (@desc) = split(//, $c->{description});
my $adesc;
my $rowc = 0;
my $count = 0;
while($count < 300 ) {
$adesc .= @desc[$count];
$rowc++;
$count++;
}
$c->{description} = $adesc;= '...';
}
最佳答案
让我们看看是什么perldoc perlfaq4
不得不说:
How do I reformat a paragraph?
Use
Text::Wrap
(part of the standard Perl distribution):use Text::Wrap; print wrap("\t", ' ', @paragraphs);
The paragraphs you give to
Text::Wrap
should not contain embedded newlines.Text::Wrap
doesn't justify the lines (flush-right).
如果你不喜欢怎么办?
访问CPAN
-
use Text::Wrap::Smart 'wrap_smart'; use feature 'say'; my $text = 'lorem ipsum dolor sit amet' x 500; my $options = { no_split => 1, # Activates 'fuzzy matching' max_msg_size => 300, }; say for wrap_smart( $text, $options );
关于Perl,计算字符并在单词末尾中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8928331/