Bonsoir,
je recherche un fonction qui permet d'afficher '...' si le titre est trop long. Je m'explique:
Imaginez sur mon site il y a un article au nom "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
J'aimerais limiter le nombre de charactères et donc obtenir :
"Lorem ipsum dolor sit ..."
Voila, merci en tout cas
C'est faisable en CSS, avec la propriété text-overflow (support).
.text {
overflow: hidden;
text-overflow: ellipsis;
}
Pour le faire en PHP:
$title = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
$shortTitle = strlen($title) > 20 ? substr($title, 0, 20)."..." : $title;
echo $shortTitle;
Bonsoir,
La fonction que tu cherche est truncate :
<?php
function truncate($string, $max_length = 30, $replacement = '', $trunc_at_space = false)
{
$max_length -= strlen($replacement);
$string_length = strlen($string);
if($string_length <= $max_length)
return $string;
if( $trunc_at_space && ($space_position = strrpos($string, ' ', $max_length-$string_length)) )
$max_length = $space_position;
return substr_replace($string, $replacement, $max_length);
}
?>
Voici deux exemples :
<?php
$str = "Suspendisse at magna non lectus lacinia gravida.";
$str = truncate($str);
echo $str; // Suspendisse at magna non lectu
?>
ou
<?php
$str = "Suspendisse at magna non lectus lacinia gravida.";
$str = truncate($str, 40, '...', true);
echo $str; // Suspendisse at magna non lectus...
?>
Source et Documentation http://code.seebz.net/p/truncate/
Merci pour vos réponses, de mon côté je vien juste d'en trouvé une "mb_strimwidth"
Merci beaucoup
@Vallyan : ce n'est pas de moi mais de seebz et je l'utilise souvent pour ça que j'en fais la pub xD (rendre à Cesar ce qui appartient à Cesar :p )