You are here:  » Limiting text in product description

Support Forum



Limiting text in product description

Submitted by dflsports on Thu, 2006-03-09 04:26 in

Hello. I am trying to figure out how to limit the text displayed the product description on the products.php page. I have tried a bunch of things I have found around the web, nothing working.

Tried some stuff with substr but no idea where to or how to put it into the script.

Thanks!

Submitted by support on Thu, 2006-03-09 08:09
Submitted by nosferatu on Wed, 2008-01-30 03:44

You need to find this code

<?php
 
if ($mainProduct["description"]): 
?>
in html/product.php around line 14 and change it to:
<?php
 
print "<p>".substr($mainProduct["description"],0,1000)."</p>";  
?>
after that youn need to set description length ex: 1000

Submitted by support on Wed, 2008-01-30 09:03

Thanks nosferatu, I was referring to the wrong script previously!

Submitted by crounauer on Wed, 2008-02-06 11:37

I recently found this on the php website and have found it quite useful so thought I would share it! It limits the amount of complete words as set in $limit unless it finds a exclamation mark(!) or full stop(.). If you just want to limit it to the number of words change this code

if(($words >= $limit) && ((substr($tok, -1) == "!")||(substr($tok, -1) == ".")))

to this
if ($words >= $limit)

<?php
function summarise($paragraph$limit){
  
$tok strtok($paragraph" ");
  
$text="";
  
$words='0';
  while(
$tok){
   
$text .= " ".$tok;
   
$words++;
   if((
$words >= $limit) && ((substr($tok, -1) == "!")||(substr($tok, -1) == ".")))
     break;
   
$tok strtok(" ");
  }
return 
ltrim($text);
}
?>

then use it like this
<?php
 
echo summarise("paragraph to be summarised"20); 
?>

Regards,
Simon.

Compare Electricals