More safety checks for Text::truncate
This commit is contained in:
parent
97058daa12
commit
3e869fd60d
@ -216,23 +216,22 @@ class Text {
|
|||||||
// Truncates the string under the limit specified by the limit parameter.
|
// Truncates the string under the limit specified by the limit parameter.
|
||||||
public static function truncate($string, $limit, $end = '...')
|
public static function truncate($string, $limit, $end = '...')
|
||||||
{
|
{
|
||||||
|
// Check if over $limit
|
||||||
// Check if string is only one word
|
if(mb_strlen($string) > $limit) {
|
||||||
if(preg_match('/\s/', $string)) {
|
|
||||||
|
|
||||||
// Append the string specified by the end parameter to the end of the string as it is over the limit.
|
// Check if string is only one word
|
||||||
$truncate = trim(mb_substr($string, 0, mb_strpos($string, ' ', $limit, 'UTF-8'), 'UTF-8')).$end;
|
if(preg_match('/\s/', $string)) {
|
||||||
|
|
||||||
} else {
|
// Append the string specified by the end parameter to the end of the string as it is over the limit.
|
||||||
$truncate = trim(mb_substr($string, 0, $limit, 'UTF-8'));
|
$truncate = trim(mb_substr($string, 0, mb_strpos($string, ' ', $limit, 'UTF-8'), 'UTF-8'));
|
||||||
|
} else {
|
||||||
// Check if string is more than limit
|
$truncate = trim(mb_substr($string, 0, $limit, 'UTF-8'));
|
||||||
if(mb_strlen($string) > $limit) {
|
|
||||||
// Append $end
|
|
||||||
$truncate = $truncate.$end;
|
|
||||||
}
|
}
|
||||||
|
$truncate = $truncate.$end;
|
||||||
|
} else {
|
||||||
|
$truncate = $string;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(empty($truncate)) {
|
if(empty($truncate)) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user