質問

I want get first 50 characters and want end this with a word. So I get first 50 characters with $dis=substr($dis, 0, 50); And I don't know how can delete latest word.

My Text:

You will find that the truth is often unpopular and the contest between agreeable fancy and disagreeable fact is unequal.

First 50 characters:

"You will find that the truth is often unpopular an"

While First 51 characters:

"You will find that the truth is often unpopular and"

(First 50 characters) - (Latest word):

"You will find that the truth is often unpopular"

役に立ちましたか?

解決

You can use the strrpos() function to find the last empty space in the string.

substr($dis, 0, strrpos($dis, " "));  

他のヒント

You can explode the shortened string by the space character, then remove the last element of the array and implode the words back into a string.

You can use array_pop (See: http://uk3.php.net/array_pop) to remove the last element of the array.

$dis    = substr($dis, 0, 50);
$words  = explode(' ', $dis);
$words  = array_pop($words);
$string = implode(' ', $words);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top