I need a function that cuts a text without cutting words, adding a "..." at the end.
I need a function that cuts a text without cutting words, adding a "..." at the end.
You can do it this way:
<?php
function text_limiter_caracter($str, $limit, $suffix = '...')
{
while (substr($str, $limit, 1) != ' ') {
$limit--;
}
if (strlen($str) <= $limit) {
return $str;
}
return substr($str, 0, $limit + 1) . $suffix;
}
$str = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
echo text_limiter_caracter($str, 30, '...') . '<br>';
echo text_limiter_caracter($str, 60, '...') . '<br>';
echo text_limiter_caracter($str, 40, '...') . '<br>';
echo text_limiter_caracter($str, 80, '...') . '<br>';
Result:
Lorem ipsum dolor sit amet, ...
Lorem ipsum dolor sit amet, consectetur adipisicing elit, ...
Lorem ipsum dolor sit amet, consectetur ...
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor ...
Another solution:
function text_limiter_caracter($str, $limit, $suffix = '...')
{
if (strlen($str) <= $limit) return $str;
$limit = strpos($str, ' ', $limit);
return substr($str, 0, $limit + 1) . $suffix;
}
$str = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
$str2 = "Lorem ipsum dolor sit amet";
echo text_limiter_caracter($str, 60) . '<br>';
echo text_limiter_caracter($str, 40) . '<br>';
echo text_limiter_caracter($str, 80) . '<br>';
echo text_limiter_caracter($str2, 100) . '<br>';
Result:
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed ...
Lorem ipsum dolor sit amet, consectetur adipisicing ...
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ...
Lorem ipsum dolor sit amet
Here's a possibility in PHP, based on in another answer :
$tamanho = 300;
$pos = strrpos( $texto.' ', ' ', $tamanho );
$retorno = substr( $texto, 0, $pos - 1 );
if ( $pos < strlen( $texto ) ) $retorno .= '...';
If you are going to use UTF-8 text, give multibyte functions mb_strlen
and mb_strrpos
An alternative containing conditionals to avoid error in the third parameter of the mb_strpos()
function.
That way you do not have to worry about the amount of characters in the original string
/**
O limite de caracteres.
*/
$limit = 10;
/**
A string original
*/
$str = 'lorem ipsum lorem1 ipsum1';
/**
Obtém a quantidade de caracters da string original
*/
$str_l = mb_strlen($str);
/**
Verifica se o limite é menor que a quantidade de caracters.
Caso o limite seja maior, a função mb_strpos() retornará erro de OffSet, por isso, essa verificação é necessária.
*/
echo (($limit < $str_l)? substr($str, 0, mb_strpos($str, ' ', $limit)).'...' : $str);
The problem with this technique or any others that use the space character is not valid in languages that do not have the space character. So it's not an internationalized solution.
I put this answer in another question as well, because I believe the question here can be considered duplicated:
How to to display part of a text stored in a TEXT column?
CSS Solution
A solution that does not require PHP, uses the features of CSS:
<style type="text/css">
.foo{
border:1px solid #000;
overflow:hidden;
max-width:150px;
max-height:20px;
white-space:nowrap;
text-overflow:ellipsis;
</style>
<div class="foo">dsfd fds dgdfg fdg fdgfdg fdg fd1132 errfgdfgf dgf6576576</div>