Merge branch 'w11_MDL-31718_m23_shortentext' of git://github.com/skodak/moodle

This commit is contained in:
Eloy Lafuente (stronk7)
2012-03-13 12:51:41 +01:00
2 changed files with 45 additions and 14 deletions
+14 -14
View File
@@ -8850,7 +8850,7 @@ function shorten_text($text, $ideal=30, $exact = false, $ending='...') {
global $CFG;
// if the plain text is shorter than the maximum length, return the whole text
if (strlen(preg_replace('/<.*?>/', '', $text)) <= $ideal) {
if (textlib::strlen(preg_replace('/<.*?>/', '', $text)) <= $ideal) {
return $text;
}
@@ -8858,7 +8858,7 @@ function shorten_text($text, $ideal=30, $exact = false, $ending='...') {
// and only tag in its 'line'
preg_match_all('/(<.+?>)?([^<>]*)/s', $text, $lines, PREG_SET_ORDER);
$total_length = strlen($ending);
$total_length = textlib::strlen($ending);
$truncate = '';
// This array stores information about open and close tags and their position
@@ -8877,19 +8877,19 @@ function shorten_text($text, $ideal=30, $exact = false, $ending='...') {
} else if (preg_match('/^<\s*\/([^\s]+?)\s*>$/s', $line_matchings[1], $tag_matchings)) {
// record closing tag
$tagdetails[] = (object)array('open'=>false,
'tag'=>strtolower($tag_matchings[1]), 'pos'=>strlen($truncate));
'tag'=>textlib::strtolower($tag_matchings[1]), 'pos'=>textlib::strlen($truncate));
// if tag is an opening tag (f.e. <b>)
} else if (preg_match('/^<\s*([^\s>!]+).*?>$/s', $line_matchings[1], $tag_matchings)) {
// record opening tag
$tagdetails[] = (object)array('open'=>true,
'tag'=>strtolower($tag_matchings[1]), 'pos'=>strlen($truncate));
'tag'=>textlib::strtolower($tag_matchings[1]), 'pos'=>textlib::strlen($truncate));
}
// add html-tag to $truncate'd text
$truncate .= $line_matchings[1];
}
// calculate the length of the plain text part of the line; handle entities as one character
$content_length = strlen(preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', ' ', $line_matchings[2]));
$content_length = textlib::strlen(preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', ' ', $line_matchings[2]));
if ($total_length+$content_length > $ideal) {
// the number of characters which are left
$left = $ideal - $total_length;
@@ -8900,14 +8900,14 @@ function shorten_text($text, $ideal=30, $exact = false, $ending='...') {
foreach ($entities[0] as $entity) {
if ($entity[1]+1-$entities_length <= $left) {
$left--;
$entities_length += strlen($entity[0]);
$entities_length += textlib::strlen($entity[0]);
} else {
// no more characters left
break;
}
}
}
$truncate .= substr($line_matchings[2], 0, $left+$entities_length);
$truncate .= textlib::substr($line_matchings[2], 0, $left+$entities_length);
// maximum length is reached, so get off the loop
break;
} else {
@@ -8924,21 +8924,21 @@ function shorten_text($text, $ideal=30, $exact = false, $ending='...') {
// if the words shouldn't be cut in the middle...
if (!$exact) {
// ...search the last occurence of a space...
for ($k=strlen($truncate);$k>0;$k--) {
if (!empty($truncate[$k]) && ($char = $truncate[$k])) {
if ($char == '.' or $char == ' ') {
for ($k=textlib::strlen($truncate);$k>0;$k--) {
if ($char = textlib::substr($truncate, $k, 1)) {
if ($char === '.' or $char === ' ') {
$breakpos = $k+1;
break;
} else if (ord($char) >= 0xE0) { // Chinese/Japanese/Korean text
$breakpos = $k; // can be truncated at any UTF-8
break; // character boundary.
} else if (strlen($char) > 2) { // Chinese/Japanese/Korean text
$breakpos = $k+1; // can be truncated at any UTF-8
break; // character boundary.
}
}
}
if (isset($breakpos)) {
// ...and cut the text in this position
$truncate = substr($truncate, 0, $breakpos);
$truncate = textlib::substr($truncate, 0, $breakpos);
}
}