【问题标题】:Short Text, PHP短文本,PHP
【发布时间】:2011-09-08 12:36:01
【问题描述】:

我得到了这个功能:

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

wwwwwwwwwwww

该函数会将其更改为:

...(3 个点)。

我不想把它改成这样:

www ...

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

【问题讨论】:

    标签: php text short


    【解决方案1】:

    我假设您只想接受输入。如果它比 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;
        }
    }
    

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

    【讨论】:

    • 谢谢!我找到的最佳解决方案!
    • 我只会使用多字节版本的 strlen 和 substr - 使函数也能够处理非拉丁字符。 ...我在我的代码中做到了。
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      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;
        }
      }
      

      【讨论】:

        【解决方案4】:
        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;
        }
        

        【讨论】:

        • 对不起@oezi。显然,当你发布你的答案时,我正在制定我的答案。
        猜你喜欢
        • 2013-04-23
        • 1970-01-01
        • 1970-01-01
        • 2012-05-04
        • 2023-03-26
        • 2019-05-05
        • 2012-08-03
        • 1970-01-01
        • 2011-09-02
        相关资源
        最近更新 更多