2011-04-11 50 views
0

可能重複:
Truncate a multibyte String to n chars
How to Truncate a string in PHP to the word closest to a certain number of characters?如何截斷整個單詞?

如:

PHP是廣泛使用的通用目的腳本語言,特別適合於Web開發,可以嵌入到HTML中。如果你是PHP的新手,並想知道它是如何工作的,

只應該顯示它的一部分。

我已經使用了substr()函數,但結果可能是PHP is a widely-used general-purp。輸出如何被截斷爲一個單詞,而不是一個單詞的一部分?

+1

這是一個嚴重的問題嗎?因爲它看起來很像自動生成的垃圾郵件.. – 2011-04-11 10:31:24

+0

和許多更多。詢問前請使用搜索功能。 – Gordon 2011-04-11 10:35:20

回答

4

您可以使用PHP的wordwrap函數。

string wordwrap (string $str ,int $width string $break = "\n",bool $cut = false) 
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'