PDA

View Full Version : How can i print the first 40 words of a Article


NiteOwl
02-28-2007, 05:32 AM
How can i print the first 40 words of a Article.

The article will be in a Variable.

$article = "this is 300 words long or whatever";

I am currently printing the first 200 characters. But, it sometimes chops a word in half.

the actual code is:
sub printall {

open(IN, "$datafile") or &dienice("Print All - Couldn't open $outfile for reading: $!");
# set a shared lock
flock(IN, LOCK_SH);
# then seek the beginning of the file
seek(IN, 0, SEEK_SET);

print qq~
<table align="center" width="600" border="1" cellspacing="0" cellpadding="10">
<tr>
<td >
<font size="+1" color="#000000" face="times new roman"><b>News Articles</b></font>
</td >
</tr>

<tr>
<td >

~;
my(@ckdata) = <IN>;

@ckdata = reverse(@ckdata);
foreach my $rec (@ckdata) {

# while (my $rec = <IN>) {
chomp($rec);
my ($num,$title,$author,$date,$article) = split(/\|/,$rec);

$article =~ s/<br>/ /g; # remove linefeeds

print qq~
<a href="news.cgi?cmd=show&what=$num" title="$date" alt="$date">$title</a><br>
~;




print substr $article, 0, 200;

#####################
#
# Right here
#
#####################





print qq~ <a href="news.cgi?cmd=show&what=$num" title="$date" alt="$date">Full Story</a>~;;
print "<br><p>";


}
close(IN);
print qq~
</td>
</tr>
</table>
<br>
<p>
~;




}









Thanks for looking.

FishMonger
02-28-2007, 06:01 AM
$forty_words = join(' ', (split(/\s/, $article))[0..39]);

Edit:
You really should look into using css. The font tag is depreciated.

NiteOwl
02-28-2007, 06:08 AM
Thanks FishMonger, exactly what i needed.