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?

- 16,866
- 6
- 40
- 43

- 9,525
- 36
- 78
- 100
-
1Could you explain in more detail what your definition of a "word" is? Are we talking only English words? Words in any language? Any sequence of letters, whether it is a real word or not? What about numbers? Punctuation? Other symbols? Is '<<<<<<<<' a word? etc... – Mark Byers Aug 21 '10 at 16:13
-
Could the input text contain new line characters? – Mark Byers Aug 21 '10 at 16:41
-
Try This Link, May help You http://stackoverflow.com/a/26098951/3944217 – Edwin Thomas Sep 29 '14 at 11:39
7 Answers
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));

- 1
- 1

- 811,555
- 193
- 1,581
- 1,452
-
1I would avoid using the wordwrap function as it will unnecessarily parse the entire string. The regular expression is the better of the two, IMO. – Matthew Aug 21 '10 at 16:23
-
@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 Aug 21 '10 at 16:38
-
the regular expression will accidentally remove the last word. I have added code to include a check. See the edit. – Kim Stacks Mar 26 '14 at 06:48
$a = explode('|', wordwrap($string, 100, '|');
print $a[0];

- 39,136
- 15
- 78
- 84
-
1This fails if the character | accidentally occurs in $string. Better use chr(0) or something like it that will never occur in the $string. – Peter Mar 07 '16 at 10:24
Try a single line code
$string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, $length));

- 1,128
- 13
- 16
// 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]. '...';
}

- 4,440
- 3
- 25
- 37
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.

- 47,584
- 11
- 86
- 98
This is a solution very similar to that of Scott Evernden, but also works if the character | accidentially occurs in the string:
$result = explode(chr(0), wordwrap($longtext, $len, chr(0)))[0];

- 2,051
- 1
- 15
- 20
i tried this simple one and it worked for me
<?php echo substr($content, 0, strpos($content, ' ', 200)); ?>

- 3,156
- 32
- 32