Merge 3e869fd60d2fd47ca224c169cca4095f20babc27 into 4e14bbac2985ca24275951be6484a0a1b04f3ace

This commit is contained in:
Hakim Zulkufli 2017-08-05 18:34:56 +00:00 committed by GitHub
commit 1eeec58c81
3 changed files with 45 additions and 3 deletions

View File

@ -28,9 +28,17 @@ class dbPages extends dbJSON
{
$dataForDb = array(); // This data will be saved in the database
$dataForFile = array(); // This data will be saved in the file
// Generate title
if( empty($args['title']) ) {
$args['title'] = Text::truncate($args['content'], 60);
// Assign the new title to the slug as well.
$args['slug'] = $args['title'];
}
$key = $this->generateKey($args['slug'], $args['parent']);
// Generate UUID
$args['uuid'] = md5(time().DOMAIN);

View File

@ -111,7 +111,15 @@ class dbPosts extends dbJSON
// Current date, format of DB_DATE_FORMAT
$currentDate = Date::current(DB_DATE_FORMAT);
// Generate title
if( empty($args['title']) ) {
$args['title'] = Text::truncate($args['content'], 60);
// Assign the new title to the slug as well.
$args['slug'] = $args['title'];
}
// Generate the database key / index
$key = $this->generateKey($args['slug']);

View File

@ -212,6 +212,32 @@ class Text {
return $cut;
}
// 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, 'UTF-8'), 'UTF-8'));
} else {
$truncate = trim(mb_substr($string, 0, $limit, 'UTF-8'));
}
$truncate = $truncate.$end;
} else {
$truncate = $string;
}
if(empty($truncate)) {
return '';
}
return $truncate;
}
// Return string length
public static function length($string)