- mb_substr: documentation ( source)
- mb_strlen: documentation ( source)
- htmlspecialchars: documentation ( source)
- trim: documentation ( source)
<?php
function cut_off_string_maybe($string, $limit)
{
if (mb_strlen($string) <= $limit)
return htmlspecialchars($string);
else
return trim(htmlspecialchars(mb_substr($string, 0, $limit, 'UTF-8'))) . '...';
}
echo cut_off_string_maybe("This is a test",4);
echo cut_off_string_maybe("Whats your problem",4);