在没有剪切单词的情况下截断php中的字符串[重复]

时间:2022-05-17 02:23:05

Possible Duplicate:
Extract a fixed number of chars from an array, just full words

可能重复:从数组中提取固定数量的字符,只需要完整的单词

I need to truncate strings when they are longer than 20 chars. Words must always stay together, so that this:

我需要在长度超过20个字符时截断字符串。单词必须始终保持在一起,这样:

say hello to my little friend.

跟我的小朋友打个招呼。

becomes this:

成为这个:

say hello to my...

跟我问好...

instead of this:

而不是这个:

say hello to my litt...

跟我的小伙伴打招呼......

I set up this function based on a comment in a very old thread. The problem with this regex is that it removes the last word of the sentence (when there are 2 words or more).

我根据一个非常旧的线程中的注释设置了这个函数。这个正则表达式的问题在于它删除了句子的最后一个单词(当有2个单词或更多单词时)。

function gen_string($string,$min=20) {
    $new = preg_replace('/\s+?(\S+)?$/','',substr($string,0,$min));
    if(strlen($new) < strlen($string)) $new .= '&hellip;';
    return $new;
}

Can someone give me a hand with the regex? Thanks!

有人可以帮我拿正则表达式吗?谢谢!

Solution by Alasdair (with a few retouches)

Alasdair的解决方案(带几个润饰)

function gen_string($string,$max=20) {
    $tok = strtok($string,' ');
    $sub = '';
    while($tok !== false && mb_strlen($sub) < $max) {
        if(strlen($sub) + mb_strlen($tok) <= $max) {
            $sub .= $tok.' ';
        } else {
            break;
        }
        $tok = strtok(' ');
    }
    $sub = trim($sub);
    if(mb_strlen($sub) < mb_strlen($string)) $sub .= '&hellip;';
    return $sub;
}

2 个解决方案

#1


1  

function gen_string($string,$max=20)
{
    $tok=strtok($string,' ');
    $string='';
    while($tok!==false && strlen($string)<$max)
    {
        if (strlen($string)+strlen($tok)<=$max)
            $string.=$tok.' ';
        else
            break;
        $tok=strtok(' ');
    }
    return trim($string).'...';
}

See it in action: CodePad

看到它的实际应用:CodePad

Or, using special chars (must have Multibyte String Functions installed):

或者,使用特殊字符(必须安装多字节字符串函数):

function gen_string($string,$max=20)
{
    $tok=strtok($string,' ');
    $string='';
    while($tok!==false && mb_strlen($string)<$max)
    {
        if (mb_strlen($string)+mb_strlen($tok)<=$max)
            $string.=$tok.' ';
        else
            break;
        $tok=strtok(' ');
    }
    return trim($string).'...';
}

#2


2  

You could probably just use wordwrap() for this, right?

你可能只是使用wordwrap(),对吗?

strstr(wordwrap($string, $min), "\n", true)

It's faster and cleaner than using a regex.

它比使用正则表达式更快更干净。

#1


1  

function gen_string($string,$max=20)
{
    $tok=strtok($string,' ');
    $string='';
    while($tok!==false && strlen($string)<$max)
    {
        if (strlen($string)+strlen($tok)<=$max)
            $string.=$tok.' ';
        else
            break;
        $tok=strtok(' ');
    }
    return trim($string).'...';
}

See it in action: CodePad

看到它的实际应用:CodePad

Or, using special chars (must have Multibyte String Functions installed):

或者,使用特殊字符(必须安装多字节字符串函数):

function gen_string($string,$max=20)
{
    $tok=strtok($string,' ');
    $string='';
    while($tok!==false && mb_strlen($string)<$max)
    {
        if (mb_strlen($string)+mb_strlen($tok)<=$max)
            $string.=$tok.' ';
        else
            break;
        $tok=strtok(' ');
    }
    return trim($string).'...';
}

#2


2  

You could probably just use wordwrap() for this, right?

你可能只是使用wordwrap(),对吗?

strstr(wordwrap($string, $min), "\n", true)

It's faster and cleaner than using a regex.

它比使用正则表达式更快更干净。