function advanced_text_formatter_trim_text in Advanced Text Formatter 2.0.x
Same name and namespace in other branches
- 8 advanced_text_formatter.module \advanced_text_formatter_trim_text()
- 7 advanced_text_formatter.module \advanced_text_formatter_trim_text()
- 2.1.x advanced_text_formatter.module \advanced_text_formatter_trim_text()
Trim text.
Parameters
string $text: The string is being trimmed.
array $options: An associative array containing:
- html: TRUE means that text is in HTML.
- max_length: The maximum number of characters the a field can be.
- word_boundary: If checked, this field be trimmed only on a word boundary.
- ellipsis: If TRUE, a "..." will be added if a field was trimmed.
Return value
string The trimmed string.
1 call to advanced_text_formatter_trim_text()
- AdvancedTextFormatter::viewElements in src/
Plugin/ Field/ FieldFormatter/ AdvancedTextFormatter.php - Builds a renderable array for a field value.
File
- ./
advanced_text_formatter.module, line 216 - Advanced Text Formatter
Code
function advanced_text_formatter_trim_text($text, $options) {
if (!isset($options['html'])) {
$options['html'] = TRUE;
}
if (mb_strlen($text) > $options['max_length']) {
$text = mb_substr($text, 0, $options['max_length']);
if (!empty($options['word_boundary'])) {
$regex = "(.*)\\b.+";
if (function_exists('mb_ereg')) {
mb_regex_encoding('UTF-8');
$found = mb_ereg($regex, $text, $matches);
}
else {
$found = preg_match("/{$regex}/us", $text, $matches);
}
if ($found) {
$text = $matches[1];
}
}
// Remove scraps of HTML entities from the end of a strings.
$text = rtrim(preg_replace('/(?:<(?!.+>)|&(?!.+;)).*$/us', '', $text));
if (!empty($options['ellipsis'])) {
$text .= t('...');
}
}
$text = trim($text);
if (!empty($options['html'])) {
$text = Html::normalize($text);
}
return $text;
}