$text= 'Montážní sada pro houpací sítě a křesla | 150 kg'; // text na testovanie
$max = 41; // limit
function shorten($text,$max) {
if( strlen($text) > $max){ // ak je string dlhsi ako limit
$orez = substr($text,0,$max); // orezeme string
$exp = (explode(' ', $orez)); // explodneme/rodzelime do pola
$orez2 = array_splice($exp, count($exp) - 1); // vymazeme posledne pole
$spolu = join( ' ', $exp) ; // spat na string
return $spolu;
}
else return $text;
}
echo shorten($text,$max);
Pomocou Javascriptu:
var text = ""; // tvoj text
var max = 40 // nastavime limit pismen
function shorten(text,max) {
return text && text.length > max ? text.slice(0,max).split(' ').slice(0, -1).join(' ') : text
}