我以前也问过类似的问题,但是我需要知道这个小小的调整是否可行。我想将一个字符串缩短到100个字符,并使用 $small = substr($big, 0, 100);
来实现这一点。然而,这只需要前100个字符,并不关心它是否拆分一个单词。
有没有什么方法可以在保证不断字的前100个字符的情况下使用一个字符串呢?
例如:
$big = "This is a sentence that has more than 100 characters in it, and I want to return a string of only full words that is no more than 100 characters!"
$small = some_function($big);
echo $small;
// OUTPUT: "This is a sentence that has more than 100 characters in it, and I want to return a string of only"
有没有一种方法可以使用 PHP 实现这一点?