从字符串中获取前 100 个字符,尊重完整单词
我以前在这里问过类似的问题,但我需要知道这个小调整是否可行。我想将字符串缩短为100个字符,并用它来执行此操作。但是,这只需要前100个字符,并且不关心它是否分解了一个单词。$small = substr($big, 0, 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做到这一点?