开发者

how to truncate a whole word? [duplicate]

This question already has answers here: Closed 11 years ago.

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?


You can use the wordwrap function of PHP.

string wordwrap ( string $str ,int $width string $break = "\n",bool $cut = false)


$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'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜