Reputation: 153
This guy: https://stackoverflow.com/a/7239020/2453678 posted a great answer here: http://jsfiddle.net/imoda/REs2Q/
Basically, if you want a elegant solution, counting the number of characters is out of question, as each letter has different weight.
The question is:
span {
display: inline-block;
border: black 1px solid;
width: 200px;
height: 40px;
overflow: hidden;
}
How to modify the above code, so at the end, if the string is shortened, three '.' are added?
Upvotes: 0
Views: 1223
Reputation: 14931
I've coded a simple function in PHP.
function limit_text($input, $n){
$array = preg_split('~\R+~', $input); // Split by newline(s)
$array = array_slice($array, 0, $n); // Get the first n parts
$output = implode('<br>', $array) . '...'; // Implode and add some dots
return $output;
}
Here's how you use it:
$str = 'span {
display: inline-block;
border: black 1px solid;
width: 200px;
height: 40px;
overflow: hidden;
}';
echo '<pre>' . limit_text($str, 2); . '</pre>';
Upvotes: 1