check if string exceeds limited characters then show '...'
conditional-statements, function, output, php, string
Solution
Use `mb_strlen()` and an `if`
$allowedlimit = 29;
if(mb_strlen($sentence)>$allowedlimit)
{
echo mb_substr($sentence,0,$allowedlimit)."....";
}
or in a simpler way... (using ternary operator)
$allowedlimit = 29;
echo (mb_strlen($sentence)>$allowedlimit) ? mb_substr($sentence,0,$allowedlimit)."...." : $sentence;
in a function:
function app_shortString($string, $limit = 32) {
return (mb_strlen($string)>$limit) ? mb_substr($string,0,$limit)." ..." : $string;
}
Problem
I want list some items in the list but upto some few characters, if the characters limit reaches then just show `...`. I have this `echo(substr($sentence,0,29));` but how put it condition ?