0

Possible Duplicates:
Truncate a multibyte String to n chars
How to Truncate a string in PHP to the word closest to a certain number of characters?

eg:

PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML. If you are new to PHP and want to get some idea of how it works,

Only a portion of it should be displayed.

I have used the substr() function, but the result may be PHP is a widely-used general-purp. How can the output be truncated to a whole word, not part of one?

Community
  • 1
  • 1
zhuanzhou
  • 2,409
  • 8
  • 33
  • 51

2 Answers2

4

You can use the wordwrap function of PHP.

string wordwrap ( string $str ,int $width string $break = "\n",bool $cut = false)
Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
1
$length = 17;
$string = 'The quick brown fox jumps over the lazy dog';

$cutPoint = stripos($string, ' ', $length - 1);

echo substr($string, 0, $cutPoint);

// outputs 'The quick brown fox'
JimmyJ
  • 4,311
  • 3
  • 27
  • 25