<?php
/**
* Cut text to specific length.
*
* @param string $str The text to cut.
* @param int $limit The maximum number of characters that must be returned.
* @param stirng $br_char The character to use for breaking the string.
* @param string $pad The string to use at the end of the cutted string.
* @return string
*/
function cut_text($str, $limit, $br_char = '', $pad = '...')
{
if (strlen($str) <= $limit) return $str;
if ($br_char === '') return substr($str, 0, $limit) . $pad;
return substr($str, 0, strrpos(substr($str, 0, $limit), $br_char)) . $pad;
}
$text1 = 'Lorem ipsum dolor sit amet, sed do eiusmod tempor incididunt ut laboredo.';
// Normal usage:
printf('%s<br>', cut_text($text1, 20));
// Using an space for break the string:
printf('%s<br>', cut_text($text1, 20, ' '));