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,20 +256,12 @@ class Text {
$string); $string);
} }
// 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 over $limit
if (mb_strlen($string) > $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)); $truncate = trim(mb_substr($string, 0, $limit, CHARSET));
}
$truncate = $truncate.$end; $truncate = $truncate.$end;
} else { } else {
$truncate = $string; $truncate = $string;

View File

@ -603,10 +603,12 @@ class Pages extends dbJSON {
// Returns string without HTML tags and truncated // 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::removeHTMLTags($text);
$tmpslug = Text::removeLineBreaks($tmpslug); $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 // Returns TRUE if there are new pages published, FALSE otherwise