短文,PHP

时间:2011-09-08 12:36:02

标签: php text short

我有这个功能:

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)它可以正常工作,但如果输入中有一些空格,否则输入看起来像:

  

wwwwwwwwwww

该功能会将其更改为:

  

...(3点)。

我不想改成这样的东西:

  

www ...

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

4 个答案:

答案 0 :(得分:11)

我假设你只是想接受一个输入。如果它长于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;
    }
}

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

答案 1 :(得分:3)

如果您正在寻找修剪某些实际文本的功能,您可能需要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;
}

答案 2 :(得分: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;
  }
}

答案 3 :(得分: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;
}