bug fix in truncate function

This commit is contained in:
Diego Najar 2018-09-03 18:46:21 +02:00
parent 335f159c78
commit 78968aaf4d
2 changed files with 9 additions and 15 deletions

View File

@ -256,26 +256,18 @@ class Text {
$string);
}
// Truncates the string under the limit specified by the limit parameter.
public static function truncate($string, $limit, $end = '...')
// Truncates the string under the limit specified by the limit parameter
public static function truncate($string, $limit, $end='...')
{
// Check if over $limit
if(mb_strlen($string) > $limit) {
// Check if string is only one word
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.
$truncate = trim(mb_substr($string, 0, mb_strpos($string, ' ', $limit, CHARSET), CHARSET));
} else {
$truncate = trim(mb_substr($string, 0, $limit, CHARSET));
}
if (mb_strlen($string) > $limit) {
$truncate = trim(mb_substr($string, 0, $limit, CHARSET));
$truncate = $truncate.$end;
} else {
$truncate = $string;
}
if(empty($truncate)) {
if (empty($truncate)) {
return '';
}

View File

@ -603,10 +603,12 @@ class Pages extends dbJSON {
// Returns string without HTML tags and truncated
private function generateSlug($text, $truncateLength=60) {
private function generateSlug($text, $truncateLength=60)
{
$tmpslug = Text::removeHTMLTags($text);
$tmpslug = Text::removeLineBreaks($tmpslug);
return Text::truncate($tmpslug, $truncateLength, '');
$tmpslug = Text::truncate($tmpslug, $truncateLength, '');
return $tmpslug;
}
// Returns TRUE if there are new pages published, FALSE otherwise