Commit e83fb804 authored by Clare Lenihan's avatar Clare Lenihan Committed by Clare Lenihan
Browse files

adding function format_text to strip tags and shorten text

removing old format_introction and changing uses to use format_text
parent 2de6d44d
......@@ -1705,7 +1705,9 @@ function str_shorten($str, $maxlen, $truncate = false) {
}
return $str;
}
/**
* creates the profile sideblock
*/
function profile_sideblock() {
global $USER;
safe_require('notification', 'internal');
......@@ -1744,4 +1746,43 @@ 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;
}
}
$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 .= '...';
}
return hsc($string);
}
?>
......@@ -491,6 +491,7 @@ function jsstrings() {
'views' => array(
'view' => array(
'confirmdeleteblockinstance',
'blocksinstructionajax',
),
),
'adminusersearch' => array(
......@@ -1962,18 +1963,6 @@ function format_whitespace($text) {
return $text;
}
/**
* Returns only the first short snippet of a user's introduction
*/
function format_introduction($introduction) {
$introduction = strip_tags($introduction);
// Note: the lengths are different to prevent chopping off just one or two characters in order to add an ellipsis
if (strlen($introduction) < 110) {
return $introduction;
}
return substr($introduction, 0, 100) . '...';
}
/**
* Given raw text (eg typed in by a user), this function cleans it up
* and removes any nasty tags that could mess up pages.
......
......@@ -136,7 +136,7 @@ if ($data) {
foreach ($data as $d) {
$d->name = display_name($d);
if (isset($d->introduction)) {
$d->introduction = format_introduction($d->introduction);
$d->introduction = format_text($d->introduction);
}
$d->messages = ($d->messages == 'allow' || is_friend($userid, $d->id) && $d->messages == 'friends' || $USER->get('admin')) ? 1 : 0;
}
......
......@@ -69,7 +69,7 @@ if ($data['data']) {
foreach ($data['data'] as &$record) {
if (isset($record['introduction'])) {
$record['introduction'] = format_introduction($record['introduction']);
$record['introduction'] = format_text($record['introduction']);
}
foreach ($otherdata as $userid => $otherrecord) {
......
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