views:

161

answers:

9

Possible Duplicate:
How to Truncate a string in PHP to the word closest to a certain number of characters?

How can I shorten a string to a maximum of 140 chars without slicing through a word.

Take the following string:

$string = "This is an example string that contains more than 140 characters. If I use PHPs substring function it will split it in the middle of this word."

Using substr($string, 0, 140) we would get something like this:

This is an example string that contains more than 140 characters. If I use PHPs substring function it will split it in the middle of this wo

Notice it sliced through the word "word".

What I need is to be able to shorten a string while preserving entire words but without going over 140 characters.

I did find the following code but even though it will preserve entire words, it does not guarantee that the entire string does not go over the 140 char limit:

function truncate($text, $length) {
   $length = abs((int)$length);
   if(strlen($text) > $length) {
      $text = preg_replace("/^(.{1,$length})(\s.*|$)/s", '\\1...', $text);
   }
   return($text);
}
+2  A: 

If the string is too long, you can first use substr to truncate the string and then a regular expression to remove the last full or partial word:

$s = substr($s, 0, (140 - 3));
$s = preg_replace('/ [^ ]*$/', ' ...', $s);

Note that you have to make the original shorter than 140 bytes because when you add the ... this could increase the length of the string beyond 140 bytes.

Mark Byers
A: 

I don't know php very well but here is how you can do it with approximative syntax

$total_length = 0;

$words = $whole_sentense->split(' ')

$word_index = 0

$final_sentense = ''

while($total_length + strlen($words[$word_index]) < 140)
{
  $final_sentense .= words[$word_index]
  $total_length += strlen($words[$word_index]
  $word_index++
}

return ($final_sentense)
Eric
A: 

You could use strrpos to find the last space character in the string:

function truncate($text, $length = 140) {
    if(strlen($text) > $length) {
        // $length - strlen($text) is used to find the last occurrence of a blank
        // UP TO the $length character in the string.
        $text = substr($text, 0, strrpos($text,' ', $length - strlen($text) ));
    }
    return $text;
}

It wouldn't add ... though. For this to work you can change the function to:

function truncate($text, $length = 140) {
    if(strlen($text) > $length) {
        $text = substr($text, 0, strrpos($text,' ', $length - strlen($text)-3)) . '...';
    }
    return $text;
}
Felix Kling
A: 

You may want to look at strtok in the php manual.

What you can do is run a loop and add the token's strlen every time until you exceed your desired length.

AvatarKava
A: 

Found the solution in a previous question as pointed out by @Jordan: http://stackoverflow.com/questions/79960/how-to-truncate-a-string-in-php-to-the-word-closest-to-a-certain-number-of-charac/79986#79986

Camsoft
A: 
//beak into 140 character chunks
$strParts = str_split( $str, 140 );

//if the first character of the second chunk is not whitespace
if( isset( $strParts[1] ) && !preg_match( '/^\s/', $strParts[1] ) {
    //strip off the last partial word from the first chunk
    $strParts[0] = preg_replace( '/\s\w+$/', '', $strParts[0] );
}

//you're done
$str = $strParts[0];
meouw
+1  A: 

This is the function Drupal uses to shorten strings without breaking up words.

//$wordsafe: set to TRUE to not truncate in middle of words
//$dots: set to TRUE to add " ..." to the end of the truncated string
function truncate_utf8($string, $len, $wordsafe = FALSE, $dots = FALSE) {

  if (strlen($string) <= $len) {
    return $string;
  }

  if ($dots) {
    $len -= 4;
  }

  if ($wordsafe) {
    $string = substr($string, 0, $len + 1); // leave one more character
    if ($last_space = strrpos($string, ' ')) { // space exists AND is not on position 0
      $string = substr($string, 0, $last_space);
    }
    else {
      $string = substr($string, 0, $len);
    }
  }
  else {
    $string = substr($string, 0, $len);
  }

  if ($dots) {
    $string .= ' ...';
  }

  return $string;
}
Jergason
A: 

How about using a regular expression to find all of the whitespace, then truncate at the the match that is largest but still less than or equal to your desired length.

function truncate($text, $length) {
    $length = abs((int)$length);

    $count = preg_match_all("(\s+)", $text, $matches, PREG_OFFSET_CAPTURE);

    while ($count > 0) {
       if ($matches[$count][0] <= $length) {
          $length = $matches[$count][0];
          break;
       }    
       $count = $count - 1;
    }

    return substr($text, 0, $length)
}
John Knoeller
A: 

Also strings are unlikely to have tabs, \t, in them so you can do this

$str = word_wrap( $str, 140, "\t" );
$str = explode( "\t", $str );
$str = $str[0];
meouw