【问题标题】:PHP Limit string output by specific charactersPHP限制特定字符输出的字符串
【发布时间】:2009-08-12 22:40:20
【问题描述】:

我正在尝试使用 PHP 限制从字符串返回的字符数。

我应用的解决方案似乎使服务器崩溃(高负载/无限循环),所以我要求替代方案。

我正在尝试找到一种解决方案,可以剪切字符串并显示特定数量的字符,但仍然尊重句子的含义,即它不会在单词中间进行剪切

我的函数调用如下:

<?php
uc_textcut(get_the_title());
?>

在我的functions.php中,这是我使用的代码(它确实崩溃了):

function uc_textcut($var) {

     $position = 60;
     $result = substr($var,$position,1);

     if ($result !=" ") {
         while($result !=" ") {
            $i = 1;
            $position = $position+$i;
            $result = substr($var,$position,1);
         }
     }

     $result = substr($var,0,$position);
     echo $result;
     echo "...";

}

我的问题是$position = 60

这个数字越高,它需要的负载就越多——就像它做一个非常慢的循环一样。

我想while() 有问题,但我想再次让访问者理解它,而不是插话。

有什么意见吗?

:) 非常感谢大家

【问题讨论】:

    标签: php character limit substr


    【解决方案1】:

    如果你只想剪切字符串,而不是在单词中间进行,你可以考虑使用wordwrap函数。

    它将返回一个由换行符分隔的字符串;因此,您必须使用 \n 作为分隔符来分解该字符串,并获取返回数组的第一个元素。


    有关更多信息和/或示例和/或其他解决方案,请参阅,例如:

    【讨论】:

    • 嗯.. 不得不说,这是一个非常聪明的想法。谢谢你的提示。我应用了它..它工作得很快,而且只需要一行代码。问候
    • @Ahmad : 不客气 :-) ; @Byron:PHP 的一大优点是总有一些东西可以发现;-)(这就是我喜欢 SO ^^ 的原因)
    【解决方案2】:

    这将在 60 个字符或 60 个字符后的第一个空格处截断,与您的初始代码相同,但效率更高:

    $position = 60;
    if(substr($var,$position,1) == " ") $position = strpos($var," ",$position);
    
    if($position == FALSE) $result = $var;
    else $result = substr($var,0,$position);
    

    【讨论】:

      【解决方案3】:
          $cutoff = 25;
          if ($i < $cutoff)
          {
              echo $str;
          }
          else
          {
              // look for a space
              $lastSpace = strrchr(substr($str,0,$cutoff)," ");
              echo substr($str,0,$lastspace);
              echo "...";
          }
      

      【讨论】:

        【解决方案4】:
        $matches = array();
        preg_match('/(^.{60,}?) /', $text, $matches);
        print_r($matches[1]);
        

        如果需要,您必须添加省略号。

        【讨论】:

          【解决方案5】:
          <?php
          
          // same as phantombrain's but in a function
          function uc_textcut($text) {
              $matches = array();
              preg_match('/(^.{60,}?) /', $text, $matches);
              if (isset($matches[1])) {
                  echo $matches[1] . "...";
              } else {
                  echo $text;
              }
          }
          
          
          // test it
          $textLong = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed tempus dui non sapien ullamcorper vel tincidunt nisi cursus. Vestibulum ultrices pharetra justo id varius.';
          $textShort = 'Lorem ipsum dolor sit amet.';
          
          uc_textcut($textLong);
          echo "\n";
          uc_textcut($textShort);
          

          ?>

          打印:

          Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed...
          Lorem ipsum dolor sit amet.
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-09-28
            • 1970-01-01
            • 2016-11-24
            • 1970-01-01
            相关资源
            最近更新 更多