1

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:

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).

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)

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;
}
Community
  • 1
  • 1
Andres SK
  • 10,779
  • 25
  • 90
  • 152
  • Duplicate [#1](http://stackoverflow.com/questions/79960/how-to-truncate-a-string-in-php-to-the-word-closest-to-a-certain-number-of-chara) [#2](http://stackoverflow.com/questions/965235/how-can-i-truncate-a-string-in-php) [#3](http://stackoverflow.com/questions/1270644/php-truncate-text-at-word-boundaries) and maybe more... – Peter Nov 27 '11 at 14:02
  • 1
    Try This Link, May help You... http://stackoverflow.com/a/26098951/3944217 – Edwin Thomas Sep 29 '14 at 11:45

2 Answers2

6

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

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

It's faster and cleaner than using a regex.

Tom van der Woerdt
  • 29,532
  • 7
  • 72
  • 105
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

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).'...';
}
Alasdair
  • 13,348
  • 18
  • 82
  • 138
  • Note that since it adds 3 periods onto the end, if you want a maximum of 20 characters you should call it with 17 as 2nd argument. Or you could remove the `.'...'` in the last line of the function. – Alasdair Nov 27 '11 at 14:19
  • It should be pretty fast, it uses strtok and breaks as soon as you get to the end. It's also very memory efficient! Gotta save those bytes! ;) – Alasdair Nov 27 '11 at 15:07
  • found an error. when the last letter in the $string is at the $max position, it removes the word. Example: gen_string('sáy hello tommy',15) returns "say hello" instead of "sáy hello tommy"... it is 15 chars long so it should return the complete string. It only happens when using special chars. Any idea why this is happening? – Andres SK Nov 27 '11 at 15:10
  • I updated the comment, it only happens when using special chars, like á or ñ – Andres SK Nov 27 '11 at 15:12
  • Fixed, but you do need Multibyte String Functions installed. There are no excuses, you need that extension if you're dealing with UTF8. – Alasdair Nov 27 '11 at 15:17