5

我得到了这个功能:

function shorter($text, $chars_limit) {
  if (strlen($text) > $chars_limit) 
    return substr($text, 0, strrpos(substr($text, 0, $chars_limit), " ")).'...';
  else return $text;
}

如果我使用echo shorter($input, 11)它可以正常工作,但如果输入中有一些空格,否则输入看起来像:

万维网

该函数将其更改为:

...(3 个点)。

我不希望它变成这样的东西:

万维网...

您对如何重建此脚本有任何想法吗?先感谢您。

4

4 回答 4

12

我假设您只想输入。如果它比 X 长,则在 X 处将其切断并添加“...”。

// Start function
function shorter($text, $chars_limit)
{
    // Check if length is larger than the character limit
    if (strlen($text) > $chars_limit)
    {
        // If so, cut the string at the character limit
        $new_text = substr($text, 0, $chars_limit);
        // Trim off white space
        $new_text = trim($new_text);
        // Add at end of text ...
        return $new_text . "...";
    }
    // If not just return the text as is
    else
    {
    return $text;
    }
}

我没有对此进行测试,但它应该可以工作。:)

于 2011-09-08T12:55:50.663 回答
5

如果您正在寻找一个修剪一些实际文本的函数,您可能需要一个 UTF-8 安全函数。此外,如果您想稍微智能地修剪文本(仅在字母数字字符之后修剪文本,没有 HTML 等),您可以尝试我写的这个功能:

/**
 * shortens the supplied text after last word
 * @param string $string
 * @param int $max_length
 * @param string $end_substitute text to append, for example "..."
 * @param boolean $html_linebreaks if LF entities should be converted to <br />
 * @return string
 */
function mb_word_wrap($string, $max_length, $end_substitute = null, $html_linebreaks = true) { 

    if($html_linebreaks) $string = preg_replace('/\<br(\s*)?\/?\>/i', "\n", $string);
    $string = strip_tags($string); //gets rid of the HTML

    if(empty($string) || mb_strlen($string) <= $max_length) {
        if($html_linebreaks) $string = nl2br($string);
        return $string;
    }

    if($end_substitute) $max_length -= mb_strlen($end_substitute, 'UTF-8');

    $stack_count = 0;
    while($max_length > 0){
        $char = mb_substr($string, --$max_length, 1, 'UTF-8');
        if(preg_match('#[^\p{L}\p{N}]#iu', $char)) $stack_count++; //only alnum characters
        elseif($stack_count > 0) {
            $max_length++;
            break;
        }
    }
    $string = mb_substr($string, 0, $max_length, 'UTF-8').$end_substitute;
    if($html_linebreaks) $string = nl2br($string);

    return $string;
}
于 2011-09-08T12:51:13.057 回答
1

假设包含空格的字符串的行为不应该改变,试试这个:

function shorter($text, $chars_limit) {
  if (strlen($text) > $chars_limit) {
    $rpos = strrpos(substr($text, 0, $chars_limit), " ");
    if ($rpos!==false) {
      // if there's whitespace, cut off at last whitespace
      return substr($text, 0, $rpos).'...'; 
    }else{
      // otherwise, just cut after $chars_limit chars
      return substr($text, 0, $chars_limit).'...'; 
    }
  } else {
    return $text;
  }
}
于 2011-09-08T12:47:00.227 回答
1
function shorter($input, $length)
{
    //no need to trim, already shorter than trim length
    if (strlen($input) <= $length) {
        return $input;
    }

    //find last space within length
    $last_space = strrpos(substr($input, 0, $length), ' ');
    if(!$last_space) $last_space = $length;
    $trimmed_text = substr($input, 0, $last_space);

    //add ellipses (...)
    $trimmed_text .= '...';

    return $trimmed_text;
}
于 2011-09-08T12:51:35.440 回答