Be careful with that, as it doesn’t detect when a word ends and when one begins so you could end up with
“Multi-position hitting tee with home pl...”
You may be better off using a function such as this…
function myTruncate($string, $limit, $break=".", $pad="...") { // return with no change if string is shorter than $limit if(strlen($string) <= $limit) return $string; // is $break present between $limit and the end of the string? if(false !== ($breakpoint = strpos($string, $break, $limit))) { if($breakpoint < strlen($string) - 1) { $string = substr($string, 0, $breakpoint) . $pad; } } return $string; }
You are running a Whiskey site and you have to ask if anyone will help!
function myTruncate($string, $limit, $break=".", $pad="...") { // return with no change if string is shorter than $limit if(strlen($string) <= $limit) return $string; // is $break present between $limit and the end of the string? if(false !== ($breakpoint = strpos($string, $break, $limit))) { if($breakpoint < strlen($string) - 1) { $string = substr($string, 0, $breakpoint) . $pad; } } return $string; }
For example on the view.phtml page you can do this
Thanks for your replies. The site belongs to my client, not me personally.
It’s difficult to please everyone when deciding how to display sections like scotch. For example there are 144 different distilleries listed in the single malt a to z - that’s for people that know what they want. Trying to list over 800 different single malts for people who don’t know one from another is quite difficult, but I take on board your suggestion. We may well look at that in due course.
Thanks for the truncate tip - what we want to do is have the full product description on the category pages (ie the list/grid pages) e.g Johnnie Walker but I have tried adding
<?php echo $_description ?>
to the list.phtml page and it just returns an error. Do you have any thoughts on how we can do this?
The reason is that we are using an invoice module that adds the short description to the invoice and we want to keep the content of the short description to a minimum.
function setShortDescription($string, $limit, $break=".", $pad="...") { // return with no change if string is shorter than $limit if(strlen($string) <= $limit) return $string; // is $break present between $limit and the end of the string? if(false !== ($breakpoint = strpos($string, $break, $limit))) { if($breakpoint < strlen($string) - 1) { $string = substr($string, 0, $breakpoint) . $pad; } } return $string; }
<?php function setShortDescription($string, $limit, $break=".", $pad="...") { // return with no change if string is shorter than $limit if(strlen($string) <= $limit) return $string; // is $break present between $limit and the end of the string? if(false !== ($breakpoint = strpos($string, $break, $limit))) { if($breakpoint < strlen($string) - 1) { $string = substr($string, 0, $breakpoint) . $pad; } } return $string; } ?>