To avoid cutting right in the middle of a word, you might want to try the wordwrap
function ; something like this, I suppose, could do :
$str = "this is a long string that should be cut in the middle of the first 'that'";
$wrapped = wordwrap($str, 25);
var_dump($wrapped);
$lines = explode("\n", $wrapped);
var_dump($lines);
$new_str = $lines[0] . '...';
var_dump($new_str);
$wrapped
will contain :
string 'this is a long string
that should be cut in the
middle of the first
'that'' (length=74)
The $lines
array will be like :
array
0 => string 'this is a long string' (length=21)
1 => string 'that should be cut in the' (length=25)
2 => string 'middle of the first' (length=19)
3 => string ''that'' (length=6)
And, finally, your $new_string
:
string 'this is a long string' (length=21)
With a substr, like this :
var_dump(substr($str, 0, 25) . '...');
You'd have gotten :
string 'this is a long string tha...' (length=28)
Which doesn't look that nice :-(
Still, have fun !