如何将字符串截断为PHP中的前20个单词?

san*_*ers 58 php string

如何在PHP中删除20个单词后的字符串?

kar*_*m79 126

function limit_text($text, $limit) {
      if (str_word_count($text, 0) > $limit) {
          $words = str_word_count($text, 2);
          $pos = array_keys($words);
          $text = substr($text, 0, $pos[$limit]) . '...';
      }
      return $text;
    }

echo limit_text('Hello here is a long sentence blah blah blah blah blah hahahaha haha haaaaaa', 5);
Run Code Online (Sandbox Code Playgroud)

输出:

Hello here is a long ...
Run Code Online (Sandbox Code Playgroud)

  • 当在一种情况下使用$ limit作为数组$ pos的单词选择器而在另一种情况下使用$ limit时,通过确定字符串是否足够长,此函数会混淆字符数和字数. (6认同)
  • `str_word_count`而不是`strlen`应该诀窍:)所以用`str_word_count($ text,0)替换`strlen($ text)` (2认同)

nop*_*ole 29

将数字更改为下面3的数字20以获取前20个单词,或将其作为参数传递.下面演示了如何得到第3个字:(所以更改320更改默认值):

function first3words($s, $limit=3) {
    return preg_replace('/((\w+\W*){'.($limit-1).'}(\w+))(.*)/', '${1}', $s);   
}

var_dump(first3words("hello yes, world wah ha ha"));  # => "hello yes, world"
var_dump(first3words("hello yes,world wah ha ha"));   # => "hello yes,world"
var_dump(first3words("hello yes world wah ha ha"));   # => "hello yes world"
var_dump(first3words("hello yes world"));  # => "hello yes world"
var_dump(first3words("hello yes world.")); # => "hello yes world"
var_dump(first3words("hello yes"));  # => "hello yes"
var_dump(first3words("hello"));  # => "hello"
var_dump(first3words("a")); # => "a"
var_dump(first3words(""));  # => ""
Run Code Online (Sandbox Code Playgroud)

  • 令人惊讶的是这是迄今为止最快速的限制词,这比爆炸性的好,做得好,做得好. (2认同)
  • 只是一个旁注:如果你的字符串中有换行符,这个函数会为每个句子每 20 个单词截断一次(假设你在每个句子后面都有一个换行符)。 (2认同)

Jac*_*nkr 13

到最近的空间

截断到目标字符的最近前空格.演示

  • $str 要截断的字符串
  • $chars 要剥离的字符数量可以被覆盖 $to_space
  • $to_space boolean是否从$chars极限空间截断

功能

function truncateString($str, $chars, $to_space, $replacement="...") {
   if($chars > strlen($str)) return $str;

   $str = substr($str, 0, $chars);
   $space_pos = strrpos($str, " ");
   if($to_space && $space_pos >= 0) 
       $str = substr($str, 0, strrpos($str, " "));

   return($str . $replacement);
}
Run Code Online (Sandbox Code Playgroud)

样品

<?php

$str = "this is a string that is just some text for you to test with";

print(truncateString($str, 20, false) . "\n");
print(truncateString($str, 22, false) . "\n");
print(truncateString($str, 24, true) . "\n");
print(truncateString($str, 26, true, " :)") . "\n");
print(truncateString($str, 28, true, "--") . "\n");

?>
Run Code Online (Sandbox Code Playgroud)

产量

this is a string tha...
this is a string that ...
this is a string that...
this is a string that is :)
this is a string that is--
Run Code Online (Sandbox Code Playgroud)


Mat*_*nes 9

使用explode().

来自文档的示例.

// Example 1
$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
Run Code Online (Sandbox Code Playgroud)

请注意,爆炸具有限制功能.所以你可以做点什么

$message = implode(" ", explode(" ", $long_message, 20));
Run Code Online (Sandbox Code Playgroud)

  • 爆炸的限制函数并不像本文中所建议的那样起作用。请参阅 http://www.php.net/manual/en/function.explode.php 示例#2,正限制。 (2认同)
  • @Tegeril是正确的。尽管可以解决此问题:`$ words = implode(“”,array_slice(explode(“”,$ t),0,$ count));` (2认同)

Ass*_*vie 7

试试正则表达式.

你需要一些能匹配20个单词(或20个单词边界)的东西.

所以(我的正则表达式非常糟糕,如果不准确的话,请纠正我):

/(\w+\b){20}/
Run Code Online (Sandbox Code Playgroud)

以下是php中正则表达式的一些示例.


小智 7

简单且完全配备的truncate()方法:

function truncate($string, $width, $etc = ' ..')
{
    $wrapped = explode('$trun$', wordwrap($string, $width, '$trun$', false), 2);
    return $wrapped[0] . (isset($wrapped[1]) ? $etc : '');
}
Run Code Online (Sandbox Code Playgroud)


小智 5

它不是我自己的创作,它是以前帖子的修改.积分去karim79.

function limit_text($text, $limit) {
    $strings = $text;
      if (strlen($text) > $limit) {
          $words = str_word_count($text, 2);
          $pos = array_keys($words);
          if(sizeof($pos) >$limit)
          {
            $text = substr($text, 0, $pos[$limit]) . '...';
          }
          return $text;
      }
      return $text;
    }
Run Code Online (Sandbox Code Playgroud)


Mik*_*yan 5

如果你在 Laravel 上编码use Illuminate\Support\Str

这是例子

Str::words($category->publication->title, env('WORDS_COUNT_HOME'), '...')
Run Code Online (Sandbox Code Playgroud)

希望这有帮助。