达到某个字符限制时,将文本块修剪为最近的单词?

时间:2022-01-20 21:26:18

Here is the question: How would your trim a block of text to the nearest word when a certain amount of characters have past. I'm not trying to limit a certain number words or letters, but limit the letters and cut it off at the nearest word.

这是一个问题:当一定数量的字符过去时,如何将一段文本修剪到最近的单词。我不是要限制一定数量的单词或字母,而是限制字母并将其剪切为最近的单词。

Say I had two strings:

说我有两个字符串:

"This is a block of text, blah blah blah"
"this is another block of txt 2 work with"

Say I wanted to limit it to 27 characters, the first line would end at "blah" and the second on would end at "txt" even though the character limits are reached within those words.

假设我想将它限制为27个字符,第一行将以“blah”结束,第二行将以“txt”结束,即使在这些单词中达到了字符限制。

Is there any clean solution to this problem?

这个问题有什么清洁的解决方案吗?

6 个解决方案

#1


See the wordwrap function.

请参阅wordwrap函数。

I would probably do something like:

我可能会这样做:

function wrap($string) {
  $wstring = explode("\n", wordwrap($string, 27, "\n") );
  return $wstring[0];
}

(If your strings already span across severeal lines, use other char - or pattern - for the split other than "\n")

(如果你的字符串已跨越严重的行,则使用其他字符 - 或模式 - 用于“\ n”以外的拆分)

#2


I wrote a max-string-length function that does just this and is very clean.

我写了一个max-string-length函数来做这个并且非常干净。

#3


Wouldn't it be simpler to concat the strings using a place holder (i.e.: ###PLACEHOLDER###), count the chars of the string minus your place holder, trim it to the right length with substr and then explode by placeholder?

使用占位符(即:### PLACEHOLDER ###)连接字符串不是更简单,计算字符串的字符减去占位符,使用substr将其修剪到正确的长度,然后通过占位符进行爆炸?

#4


I think this should do the trick:

我认为这应该做的伎俩:

function trimToWord($string, $length, $delimiter = '...')
{
    $string        = str_replace("\n","",$string);
    $string        = str_replace("\r","",$string);
    $string        = strip_tags($string);
    $currentLength = strlen($string);

    if($currentLength > $length)
    {
        preg_match('/(.{' . $length . '}.*?)\b/', $string, $matches);

        return rtrim($matches[1]) . $delimiter;
    }
    else 
    {
        return $string;
    }
}

#5


You can use a little-known modifier to str_word_count to help do this. If you pass the parameter '2', it returns an array of where the word position are.

你可以使用一个鲜为人知的mod_word_count修饰符来帮助你做到这一点。如果传递参数'2',它将返回一个数字位置所在的数组。

The following is a simple way of using this, but it might be possible to do it more efficiently:

以下是使用它的简单方法,但可以更有效地执行此操作:

$str = 'This is a string with a few words in';
$limit = 20;
$ending = $limit;

$words = str_word_count($str, 2);

foreach($words as $pos=>$word) {
    if($pos+strlen($word)<$limit) {
        $ending=$pos+strlen($word);
    }
    else{
        break;
    }
}

echo substr($str, 0, $ending);
// outputs 'this is a string'

#6


// Trim very long text to 120 characters. Add an ellipsis if the text is trimmed.
if(strlen($very_long_text) > 120) {
  $matches = array();
  preg_match("/^(.{1,120})[\s]/i", $very_long_text, $matches);
  $trimmed_text = $matches[0]. '...';
}

#1


See the wordwrap function.

请参阅wordwrap函数。

I would probably do something like:

我可能会这样做:

function wrap($string) {
  $wstring = explode("\n", wordwrap($string, 27, "\n") );
  return $wstring[0];
}

(If your strings already span across severeal lines, use other char - or pattern - for the split other than "\n")

(如果你的字符串已跨越严重的行,则使用其他字符 - 或模式 - 用于“\ n”以外的拆分)

#2


I wrote a max-string-length function that does just this and is very clean.

我写了一个max-string-length函数来做这个并且非常干净。

#3


Wouldn't it be simpler to concat the strings using a place holder (i.e.: ###PLACEHOLDER###), count the chars of the string minus your place holder, trim it to the right length with substr and then explode by placeholder?

使用占位符(即:### PLACEHOLDER ###)连接字符串不是更简单,计算字符串的字符减去占位符,使用substr将其修剪到正确的长度,然后通过占位符进行爆炸?

#4


I think this should do the trick:

我认为这应该做的伎俩:

function trimToWord($string, $length, $delimiter = '...')
{
    $string        = str_replace("\n","",$string);
    $string        = str_replace("\r","",$string);
    $string        = strip_tags($string);
    $currentLength = strlen($string);

    if($currentLength > $length)
    {
        preg_match('/(.{' . $length . '}.*?)\b/', $string, $matches);

        return rtrim($matches[1]) . $delimiter;
    }
    else 
    {
        return $string;
    }
}

#5


You can use a little-known modifier to str_word_count to help do this. If you pass the parameter '2', it returns an array of where the word position are.

你可以使用一个鲜为人知的mod_word_count修饰符来帮助你做到这一点。如果传递参数'2',它将返回一个数字位置所在的数组。

The following is a simple way of using this, but it might be possible to do it more efficiently:

以下是使用它的简单方法,但可以更有效地执行此操作:

$str = 'This is a string with a few words in';
$limit = 20;
$ending = $limit;

$words = str_word_count($str, 2);

foreach($words as $pos=>$word) {
    if($pos+strlen($word)<$limit) {
        $ending=$pos+strlen($word);
    }
    else{
        break;
    }
}

echo substr($str, 0, $ending);
// outputs 'this is a string'

#6


// Trim very long text to 120 characters. Add an ellipsis if the text is trimmed.
if(strlen($very_long_text) > 120) {
  $matches = array();
  preg_match("/^(.{1,120})[\s]/i", $very_long_text, $matches);
  $trimmed_text = $matches[0]. '...';
}