Commit 1a734f47 authored by Clare Lenihan's avatar Clare Lenihan Committed by Clare Lenihan
Browse files

merging format_text and str_shorten under the name str_shorten and moving it to lib/web.php

parent b2fcdcea
......@@ -251,7 +251,7 @@ $smarty->display('interaction:forum:view.tpl');
function setup_topics(&$topics) {
if ($topics) {
foreach ($topics as $topic) {
$topic->body = format_text($topic->body, 50, 10, false);
$topic->body = str_shorten($topic->body, 50, false, false);
$topic->lastposttime = relative_date(get_string('strftimerecentrelative', 'interaction.forum'), get_string('strftimerecent'), $topic->lastposttime);
}
}
......
......@@ -345,7 +345,7 @@ function setup_groups($groups, $returnto='mygroups') {
}
$i = 0;
foreach ($groups as $group) {
$group->description = format_text($group->description);
$group->description = str_shorten($group->description, 100, false);
if ($group->type == 'member') {
$group->canleave = group_user_can_leave($group->id);
}
......
......@@ -1687,24 +1687,6 @@ function display_size($size) {
return $size;
}
/**
* Takes a string and a length, and ensures that the string is no longer than
* this length, by putting '...' in the middle
*
* @param string $str String to shorten
* @param int $maxlen The maximum length the new string should be
* @param bool $truncate if true, cut the string at the end rather than in the middle
* @return string
*/
function str_shorten($str, $maxlen, $truncate = false) {
if ($truncate && strlen($str) > $maxlen) {
return substr($str, 0, $maxlen-3) . '...';
}
if (strlen($str) > $maxlen) {
return substr($str, 0, floor($maxlen / 2) - 1) . '...' . substr($str, -(floor($maxlen / 2) - 2));
}
return $str;
}
/**
* creates the profile sideblock
*/
......@@ -1746,49 +1728,4 @@ function profile_sideblock() {
return $data;
}
/**
* strips the tags and reduces the length of some text
*
* @param string $string the text to format
* @param int $length the number of characters desired (default 100)
* @param int $extra the max size on top of the length (default 10)
* @param boolean $newlines whether to include text past the first newline (default true)
*/
function format_text($string, $length=100, $extra=10, $newlines=true) {
$more = false;
if (!$newlines) {
$nextbreak = strpos($string, '<p', 1);
if ($nextbreak !== false) {
$string = substr($string, 0, $nextbreak);
$more = true;
}
$nextbreak = strpos($string, '<br', 1);
if ($nextbreak !== false) {
$string = substr($string, 0, $nextbreak);
$more = true;
}
}
// so newlines don't disappear.
$string = str_replace('<p', "%p<p", $string);
$string = str_replace('<br', "%br<br", $string);
$string = strip_tags($string);
$string = html_entity_decode($string); // no things like &nbsp; only take up one character
// take the first $length chars, then up to the first space (max length $length + $extra chars)
if (strlen($string) > $length + $extra) {
$string = substr($string, 0, $length + $extra);
$nextspace = strpos($string, ' ', $length);
if ($nextspace !== false) {
$string = substr($string, 0, $nextspace);
}
$more = true;
}
if ($more) {
$string .= '...';
}
$string = hsc($string);
$string = str_replace('%p', '<p>', $string);
return str_replace('%br', '<br>', $string);
}
?>
......@@ -839,7 +839,7 @@ function get_users_data($userlist) {
foreach ($data as &$record) {
if (isset($record->introduction)) {
$record->introduction = format_text($record->introduction);
$record->introduction = str_shorten($record->introduction, 100, false);
}
$record->messages = ($record->messages == 'allow' || $record->friend && $record->messages == 'friends' || $USER->get('admin')) ? 1 : 0;
......
......@@ -1995,6 +1995,45 @@ function display_cleaned_html($html, $filename, $params) {
exit;
}
/**
* Takes a string and a length, and ensures that the string is no longer than
* this length, by putting '...' in the middle
* Strips all tags except <br> and <p>
*
* @param string $str String to shorten
* @param int $maxlen The maximum length the new string should be (default 100)
* @param bool $truncate if true, cut the string at the end rather than in the middle (default false)
* @param bool $newlines if false, cut off after the first newline (default true)
* @return string
*/
function str_shorten($str, $maxlen=100, $truncate=false, $newlines=true) {
if (!$newlines) {
$nextbreak = strpos($str, '<p', 1);
if ($nextbreak !== false) {
$str = substr($str, 0, $nextbreak);
}
$nextbreak = strpos($str, '<br', 1);
if ($nextbreak !== false) {
$str = substr($str, 0, $nextbreak);
}
}
// so newlines don't disappear but ignore the first <p>
$str = $str[0] . str_replace('<p', "\n\n<p", substr($str, 1));
$str = str_replace('<br', "\n<br", $str);
$str = strip_tags($str);
$str = html_entity_decode($str); // no things like &nbsp; only take up one character
// take the first $length chars, then up to the first space (max length $length + $extra chars)
if ($truncate && strlen($str) > $maxlen) {
$str = substr($str, 0, $maxlen-3) . '...';
}
if (strlen($str) > $maxlen) {
$str = substr($str, 0, floor($maxlen / 2) - 1) . '...' . substr($str, -(floor($maxlen / 2) - 2));
}
return nl2br(hsc($str));
}
/**
* Builds pagination links for HTML display.
......
......@@ -108,7 +108,7 @@ if ($allviews = get_records_array('view', 'owner', $userid)) {
if (can_view_view($view->id)) {
$views[$view->id] = $view;
$view->artefacts = array();
$view->description = format_text($view->description);
$view->description = str_shorten($view->description, 100, false);
}
}
}
......@@ -148,7 +148,7 @@ if (!$userassocgroups = get_associated_groups($userid, false)) {
}
foreach ($userassocgroups as $group) {
$group->description = format_text($group->description);
$group->description = str_shorten($group->description, 100, false);
}
if (is_postgres()) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment