3v4l.org

run code in 300+ PHP versions simultaneously
<?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);

preferences:
40.93 ms | 402 KiB | 5 Q