【问题标题】:Limit String Length限制字符串长度
【发布时间】:2010-06-10 23:32:10
【问题描述】:

我正在寻找一种方法来限制 php 中的字符串,如果字符串太长,则在末尾添加 ...。

【问题讨论】:

标签: php string


【解决方案1】:

你可以使用类似下面的东西:

if (strlen($str) > 10)
   $str = substr($str, 0, 7) . '...';

【讨论】:

  • 我猜你不认为他可能会跳过他的家庭作业阅读作业。
  • 对不起,我没有考虑到这一点。家庭作业的规则是什么?哦,等等,我刚刚注意到,我的代码甚至无法正常工作;)
  • 这只是一个社区规则:meta.stackexchange.com/questions/10811/…
  • 为了获取 UTF-8 字符的子字符串,我强烈推荐 mb_substr -> mb_substr($utf8string,0,5,'UTF-8'); 这样可以避免切片长度错误。
【解决方案2】:

php 4.0.6 开始,有一个完全相同的功能

函数 mb_srimwidth 可用于您的要求

<?php
echo mb_strimwidth("Hello World", 0, 10, "...");
//Hello W...
?>

不过,它确实有更多选项,这是mb_strimwidth 的文档

【讨论】:

  • wordwrap() 函数也很有用:w3schools.com/php/func_string_wordwrap.asp
  • 但就个人而言,我更喜欢使用:$s=preg_replace('/\s+?(\S+)?$/', '', substr($s, $START, $LIMIT)) ;
  • @CyrilJacquart 最好避免在不必要的地方使用正则表达式。
【解决方案3】:

如果您不想拆分单词,您可以使用wordwrap() 函数然后在换行符上展开并取第一部分。

$str = 'Stack Overflow is as frictionless and painless to use as we could make it.';
$str = wordwrap($str, 28);
$str = explode("\n", $str);
$str = $str[0] . '...';

来源:https://stackoverflow.com/a/1104329/1060423

如果您不关心拆分单词,那么只需使用php substr function

echo substr($str, 0, 28) . '...';

【讨论】:

    【解决方案4】:

    第二个参数是从哪里开始字符串,第三个参数是你需要显示多少个字符

    $title = "This is for testing string for get limit of string This is for testing string for get limit of string This is for testing string for get limit of string This is for testing string for get limit of string";
        echo substr($title,0,50);
    

    【讨论】:

      【解决方案5】:

      使用php online manual's string functions 做一些功课。 您需要在比较设置中使用strlen,如果需要,可以使用substr 进行剪切,以及使用"...""&amp;hellip;" 的连接运算符

      【讨论】:

        【解决方案6】:
         $string = (strlen($string) > 13) ? substr($string,0,10).'...' : $string;
        

        或作为函数:

        function truncate($string, $length, $dots = "...") {
            return (strlen($string) > $length) ? substr($string, 0, $length - strlen($dots)) . $dots : $string;
        }
        

        我写这个答案已经有一段时间了,我实际上不再使用这个代码了。我更喜欢这个函数,它可以防止使用 wordwrap 函数破坏单词中间的字符串:

        function truncate($string,$length=100,$append="&hellip;") {
        $string = trim($string);
        
          if(strlen($string) > $length) {
            $string = wordwrap($string, $length);
            $string = explode("\n", $string, 2);
            $string = $string[0] . $append;
          }
        
          return $string;
        }
        

        【讨论】:

          【解决方案7】:

          在 Laravel 中,有一个 string util 函数,它是这样实现的:

          public static function limit($value, $limit = 100, $end = '...')
          {
              if (mb_strwidth($value, 'UTF-8') <= $limit) {
                  return $value;
              }
          
              return rtrim(mb_strimwidth($value, 0, $limit, '', 'UTF-8')).$end;
          }
          

          【讨论】:

            【解决方案8】:

            要在不破坏单词的情况下截断最大限制提供的字符串,请使用:

            /**
             * truncate a string provided by the maximum limit without breaking a word
             * @param string $str
             * @param integer $maxlen
             * @return string
             */
            public static function truncateStringWords($str, $maxlen): string
            {
                if (strlen($str) <= $maxlen) return $str;
            
                $newstr = substr($str, 0, $maxlen);
                if (substr($newstr, -1, 1) != ' ') $newstr = substr($newstr, 0, strrpos($newstr, " "));
            
                return $newstr;
            }
            

            【讨论】:

              【解决方案9】:

              以另一种方式限制 php 中的字符串并使用下面的代码添加阅读更多文本或类似“...”

              if (strlen(preg_replace('#^https?://#', '', $string)) > 30) { 
                  echo substr(preg_replace('#^https?://#', '', $string), 0, 35).'&hellip;'; 
              }
              

              【讨论】:

                【解决方案10】:
                $res = explode("\n",wordwrap('12345678910', 8, "...\n",true))[0];
                
                // $res will be  : "12345678..."
                

                【讨论】:

                • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
                【解决方案11】:
                function truncateString($string, $maxlength, $ellipsis = false){
                
                    if(mb_strlen($string) <= $maxlength){
                        return $string;
                    }
                
                    if(empty($ellipsis)){
                        $ellipsis = '';
                    }
                
                    if($ellipsis === true){
                        $ellipsis = '…';
                    }
                
                    $ellipsis_length = mb_strlen($ellipsis);
                
                    $maxlength = $maxlength - $ellipsis_length;
                
                    $string = trim(mb_substr($string, 0, $maxlength)) . $ellipsis;
                
                    return $string;
                
                }
                

                http://sandbox.onlinephpfunctions.com/code/968e2fd1e98b60828a12d6dc0b68ec38b3628757

                【讨论】:

                  【解决方案12】:

                  $value = str_limit('这个字符串真的很长。', 7);

                  // 这句...

                  【讨论】:

                  • 这是 Laravel 的东西
                  猜你喜欢
                  • 2011-04-19
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2011-01-09
                  相关资源
                  最近更新 更多