Limit String Length
You can use the wordwrap()
function then explode on newline and take the first part, if you don't want to split words.
$str = 'Stack Overflow is as frictionless and painless to use as we could make it.';
$str = wordwrap($str, 28);
$str = explode("\n", $str);
$str = $str[0] . '...';
Source: https://stackoverflow.com/a/1104329/1060423
If you don't care about splitting words, then simply use the php substr function.
echo substr($str, 0, 28) . '...';
From php 4.0.6 , there is a function for the exact same thing
function mb_strimwidth can be used for your requirement
<?php
echo mb_strimwidth("Hello World", 0, 10, "...");
//Hello W...
?>
It does have more options though,here is the documentation for this mb_strimwidth
You can use something similar to the below:
if (strlen($str) > 10)
$str = substr($str, 0, 7) . '...';