欢迎各位兄弟 发布技术文章
这里的技术是共享的
truncate_utf8($string, $len, $wordsafe = FALSE, <ins>$dots</ins> = FALSE)
常用的截取字符串函数,参数说明:
$string:待截取的字符串;$len:要截取的长度;$wordsafe:截取时是否保持字词完整;$dots:是否在末尾添加省略号“...”。
//这个函数是drupal本身自带的函数
function truncate_utf8($string, $len, $wordsafe = FALSE, $dots = FALSE) {
if (drupal_strlen($string) <= $len) {
return $string;
}
if ($dots) {
$len -= 4;
}
if ($wordsafe) {
$string = drupal_substr($string, 0, $len + 1); // leave one more character
if ($last_space = strrpos($string, ' ')) { // space exists AND is not on position 0
$string = substr($string, 0, $last_space);
}
else {
$string = drupal_substr($string, 0, $len);
}
}
else {
$string = drupal_substr($string, 0, $len);
}
if ($dots) {
$string .= ' ...';
}
return $string;
}
//这个函数是drupal本身自带的函数 字符串长度
function drupal_strlen($text) { global $multibyte; if ($multibyte == UNICODE_MULTIBYTE) { return mb_strlen($text); } else { // Do not count UTF-8 continuation bytes. return strlen(preg_replace("/[\x80-\xBF]/", '', $text)); } }