这是我在模板中用于单词修剪的功能
<?php
/**
* Trim a string to a given number of words
*
* @param $string
* the original string
* @param $count
* the word count
* @param $ellipsis
* TRUE to add "..."
* or use a string to define other character
* @param $node
* provide the node and we'll set the $node->
*
* @return
* trimmed string with ellipsis added if it was truncated
*/
function word_trim($string, $count, $ellipsis = FALSE){
$words = explode(' ', $string);
if (count($words) > $count){
array_splice($words, $count);
$string = implode(' ', $words);
if (is_string($ellipsis)){
$string .= $ellipsis;
}
elseif ($ellipsis){
$string .= '…';
}
}
return $string;
}
?>
在页面本身中它看起来像这样
<?php echo word_trim(get_the_excerpt(), 12, ''); ?>
我想知道,有没有办法可以修改该函数来修剪字符数而不是单词数?因为有时当有更长的单词时,它会全部偏移并且不对齐。
谢谢