【问题标题】:Shorten a text string in PHP在 PHP 中缩短文本字符串
【发布时间】:2013-04-23 03:01:03
【问题描述】:

有没有办法在 PHP 中修剪文本字符串,使其具有一定数量的字符?例如,如果我有字符串:

$string = "this is a string";

我怎么能修剪它说:

$newstring = "this is";

这是我目前使用的chunk_split(),但它不起作用。谁能改进我的方法?

function trimtext($text)
{
$newtext = chunk_split($text,15);
return $newtext;
}

我也看了this的问题,但不是很懂。

【问题讨论】:

  • 不,我没有使用正则表达式。
  • 接受的答案不是正则表达式解决方案:)
  • Imulsion,那里的答案比下面的要好得多。下面的答案将切断这个词,你可能会得到像this is a str 这样的东西,这不是你想要的。因此我提供的链接中的答案是合适的,它不会在中间切断字符串,只需删除$theExcerpt .= '...'; 部分。

标签: php


【解决方案1】:
if (strlen($yourString) > 15) // if you want...
{
    $maxLength = 14;
    $yourString = substr($yourString, 0, $maxLength);
}

会做的。

看看here

【讨论】:

  • 能否解释一下第二个参数?
  • @imulsion:第二个参数是“剪切”字符串的长度)
  • 谢谢!当它让我接受时。愚蠢的十分钟等待时间
【解决方案2】:

substr 将单词切成两半。此外,如果 word 包含 UTF8 字符,则它行为不端。所以最好使用mb_substr:

$string = mb_substr('word word word word', 0, 10, 'utf8').'...';

【讨论】:

    【解决方案3】:

    您没有说出这样做的原因,但请考虑您想要实现的目标。这是一个逐字缩短字符串的函数,可以在末尾添加或不添加省略号:

    function limitStrlen($input, $length, $ellipses = true, $strip_html = true) {
        //strip tags, if desired
        if ($strip_html) {
            $input = strip_tags($input);
        }
    
        //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 !== false) {
            $trimmed_text = substr($input, 0, $last_space);
        } else {
            $trimmed_text = substr($input, 0, $length);
        }
        //add ellipses (...)
        if ($ellipses) {
            $trimmed_text .= '...';
        }
    
        return $trimmed_text;
    }
    

    【讨论】:

      【解决方案4】:
      function trimtext($text, $start, $len)
      {
          return substr($text, $start, $len);
      }
      

      你可以这样调用函数:

      $string = trimtext("this is a string", 0, 10);
      

      会返回:

      This is a

      【讨论】:

        【解决方案5】:

        substr 让您获取包含所需字符数的字符串的一部分。

        【讨论】:

          【解决方案6】:

          你可以用这个

          substr()
          

          获取子字符串的函数

          【讨论】:

            【解决方案7】:

            如果你想得到一个包含一定数量字符的字符串,你可以使用substr,即

            $newtext = substr($string,0,$length); 
            

            其中 $length 是新字符串的给定长度。

            【讨论】:

              【解决方案8】:

              如果您想要前 10 个单词的摘要(您可以在 $text 中使用 html,在脚本之前有 strip_tags) 使用此代码:

              preg_match('/^([^.!?\s]*[\.!?\s]+){0,10}/', strip_tags($text), $abstract);
              echo $abstract[0];
              

              【讨论】:

                【解决方案9】:

                我的函数有点长,但我喜欢使用它。我将字符串 int 转换为 Array。

                function truncate($text, $limit){
                    //Set Up
                    $array = [];
                    $count = -1;
                    //Turning String into an Array
                    $split_text = explode(" ", $text);
                    //Loop for the length of words you want
                    while($count < $limit - 1){
                      $count++;
                      $array[] = $split_text[$count];
                    }
                    //Converting Array back into a String
                    $text = implode(" ", $array);
                
                    return $text." ...";
                
                  }
                

                或者如果文本来自编辑器并且您想要去除 HTML 标记。

                function truncate($text, $limit){
                    //Set Up
                    $array = [];
                    $count = -1;
                    $text = filter_var($text, FILTER_SANITIZE_STRING);
                
                    //Turning String into an Array
                    $split_text = preg_split('/\s+/', $text);
                    //Loop for the length of words you want
                    while($count < $limit){
                      $count++;
                      $array[] = $split_text[$count];
                    }
                
                    //Converting Array back into a String
                    $text = implode(" ", $array);
                
                    return $text." ...";
                
                  }
                

                【讨论】:

                  【解决方案10】:

                  只有在更长的情况下使用省略号 (...) - 并注意特殊的语言特定字符:

                  mb_strlen($text,'UTF-8') > 60 ? mb_substr($text, 0, 60,'UTF-8') . "…" : $text;
                  

                  【讨论】:

                    猜你喜欢
                    • 2011-04-28
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2011-08-11
                    • 1970-01-01
                    • 1970-01-01
                    • 2017-11-11
                    • 1970-01-01
                    相关资源
                    最近更新 更多