tags:

views:

43

answers:

3

When I use substr($string,0,100), it gives first 100 characters. Sometimes it left the last word incomplete. That looks odd. Can I do limit by word rather than char?

+2  A: 

If you just count the words the resulting sting could still be very long as a single "word" might have 30 characters or more. I would suggest instead truncating the text to 100 characters, except if this causes a word to be truncated then you should also remove the truncated part of the word. This is covered by this related question:

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

Using wordwrap

$your_desired_width = 100;
if (strlen($string) > $your_desired_width)
{
    $string = wordwrap($string, 100);
    $i = strpos($string, "\n");
    if ($i) {
        $string = substr($string, 0, $i);
    }
}

This is a modified versions of the answer here. if the input text could be very long you can add this line before the call to wordwrap to avoid wordwrap having to parse the entire text:

$string = substr($string, 0, 101);

Using a regular expression (Source)

$string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, 100));
Mark Byers
I would avoid using the wordwrap function as it will unnecessarily parse the entire string. The regular expression is the better of the two, IMO.
konforce
@konforce: I agree the regular expression solution is simple and flexible but some people aren't comfortable with regular expressions. I have proposed a new version of the wordwrap solution. Is it OK now?
Mark Byers
A: 

I would do something like:

<?php

        function cut_text($text, $len)
        {
                for ($i = 0; $i < 10; ++$i)
                {
                        $c = $text[$len + $i];
                        if ($c == ' ' || $c == "\t" || $c == "\r" || $c == "\n" || $c == '-')
                                break;
                }

                if ($i == 10) $i = 0;

                return rtrim(substr($text, 0, $len + $i));
        }

        echo cut_text("Hello, World!", 3)."\n";
?>

Just start at some point ($len) and move forward a certain number of characters ($i) looking for a breaking character (e.g., whitespace). You could also look backward (-$i) or in both directions, depending on what you are looking for.

konforce
A: 
$a = explode('|', wordwrap($string, 100, '|');
print $a[0];
Scott Evernden