Web Hosting Talk







View Full Version : perl: limit characters from variable


ednit
12-25-2004, 10:55 AM
I have info from a database that I want to limit the characters that are displayed from the variable. . . . if that makes sense.

This is what I have:

$loj = "select * from products where cid1=$cid or cid2=$cid or cid3=$cid or cid4=$cid or cid5=$cid limit $low,$high";
$wer = $dbh->prepare($loj);
$wer->execute();
while ($we = $wer->fetchrow_hashref()) {
$pid = $we->{'pid'};
$brand = $we->{'brand'};
$title = $we->{'title'};
$pic = $we->{'pic'};
$price = $we->{'price'};
$quantity = $we->{'quantity'};
$weight = $we->{'weight'};
$description = $we->{'description'};
$sale = $we->{'sale'};
$saleprice = $we->{'sale_price'};

@tip = split(/\./,$pic);

if ($tip[1] =~ /jpg/) {$pic = "$tip[0]_tmb.$tip[1]";}
elsif ($tip[1] =~ /JPG/) {$pic = "$tip[0]_tmb.jpg";}
else {$pic = $pic;}


if ($sale eq "yes") {$saled = "<strong><font color=red><small>*on sale*</small></font></strong>";}
else {$saled = "";}

if ($sale eq "yes") {$price = $saleprice;}
else {$price = $price;}

if ($quantity <= 0) {

$STOCKOUT = "<font color=red><strong><small><i>*Out Of Stock</i></small></strong></font>"; }

else {$STOCKOUT = ""; }

$description =~ s/<(.*?)>//gi;
$description =~ m/^\w{25}/;


That is my code. What I want to do is limit the description to 25 words. I know nothing about regular expressions, what I did above is (I think) stripped the html out of the description, and the other thing I am trying to limit the printed text to 25 words. . .

If I need to clarify please let me know. I tried to post an example (as a link) , but it wouldnt let me cuz I don't have 5 posts here. . .

Thanks for any help.

deadserious
12-26-2004, 01:03 AM
@words = split( /\s+/, $description );
print join( ' ', @words[0..24] );


There's one way of doing it.

:)

ednit
12-26-2004, 03:51 AM
Thanks for your help, it worked wonderfully.