Fix issue with detailed counting of characters in blog - fixes BT#12324

remotes/angel/1.11.x
Yannick Warnier 8 years ago
parent 47c11fc515
commit 7b1b31fe5a
  1. 3
      main/inc/lib/api.lib.php
  2. 23
      main/inc/lib/blog.lib.php

@ -611,6 +611,9 @@ define('ADD_THEMATIC_PLAN', 6);
// Max online users to show per page (whoisonline)
define('MAX_ONLINE_USERS', 12);
// Number of characters maximum to show in preview of course blog posts
define('BLOG_MAX_PREVIEW_CHARS', 800);
// Make sure the CHAMILO_LOAD_WYSIWYG constant is defined
// To remove CKeditor libs from HTML, set this constant to true before loading
if (!defined('CHAMILO_LOAD_WYSIWYG')) {

@ -1001,7 +1001,7 @@ class Blog
'autor' => $blog_post['firstname'].' '.$blog_post['lastname'],
'username' => $blog_post['username'],
'title' => stripslashes($blog_post['title']),
'extract' => self::getPostExtract($blog_post['full_text'], 800),
'extract' => self::getPostExtract($blog_post['full_text'], BLOG_MAX_PREVIEW_CHARS),
'content' => stripslashes($blog_post['full_text']),
'post_date' => api_convert_and_format_date($blog_post['date_creation']),
'n_comments' => $blog_post_comments['number_of_comments'],
@ -3073,19 +3073,30 @@ class Blog
* @param int $length
* @return null|string
*/
private static function getPostExtract($fullText, $length = 800)
private static function getPostExtract($fullText, $length = BLOG_MAX_PREVIEW_CHARS)
{
// Remove any HTML from the string
$text = strip_tags($fullText);
$text = api_html_entity_decode($text);
// Replace end of lines with spaces
$text = preg_replace('/\s+/', ' ', $text);
$text = trim($text);
if (strlen($text) <= $length) {
// Count whitespaces to add to the cut() call below
$countBlanks = substr_count($text, ' ');
// Get a version of the string without spaces for comparison purposes
$textWithoutBlanks = str_replace(' ', '', $text);
// utf8_decode replaces non-ISO chars by '?' which avoids counting
// multi-byte characters as more than one character
$stringLength = strlen(utf8_decode($textWithoutBlanks));
if ($stringLength <= $length) {
return null;
}
$extract = cut($text, $length);
// Cut the string to the BLOG_MAX_PREVIEX_CHARS limit, adding
// whitespaces
$extract = cut($text, $stringLength + $countBlanks);
// Return an HTML string for printing
return api_htmlentities($extract);
}
}

Loading…
Cancel
Save