Drupal字符截取函数views_trim_text

Drupal的Views模块有一个很好用的字符截取函数views_trim_text,它的用法如下:

views_trim_text($alter, $value)

参数:
$alter

  • max_length: 字符串最大长度,超出部分将被截取
  • word_boundary: 以单词(Word)为边界来截取
  • ellipsis: 以单词(Word)为边界来截取,结尾以‘...’结束
  • html: 确保html标签的完整性

源代码:

<?php
function views_trim_text($alter, $value) {
  if (drupal_strlen($value) > $alter['max_length']) {
    $value = drupal_substr($value, 0, $alter['max_length']);
    // TODO: replace this with cleanstring of ctools
    if (!empty($alter['word_boundary'])) {
      $regex = "(.*)\b.+";
      if (function_exists('mb_ereg')) {
        mb_regex_encoding('UTF-8');
        $found = mb_ereg($regex, $value, $matches);
      }
      else {
        $found = preg_match("/$regex/us", $value, $matches);
      }
      if ($found) {
        $value = $matches[1];
      }
    }
    // Remove scraps of HTML entities from the end of a strings
    $value = rtrim(preg_replace('/(?:<(?!.+>)|&(?!.+;)).*$/us', '', $value));

    if (!empty($alter['ellipsis'])) {
      $value .= '...';
    }
  }
  if (!empty($alter['html'])) {
    $value = _filter_htmlcorrector($value);
  }

  return $value;
}
?>

使用实例:

$alter['max_length']=15;
$alter['ellipsis']=true;
$trim_title= views_trim_text($alter,$links->title);