相关疑难解决方法(0)

从字符串中获取前100个字符,尊重完整的单词

我之前在这里问过类似的问题,但我需要知道这个小调整是否可行.我想将一个字符串缩短为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"
Run Code Online (Sandbox Code Playgroud)

有没有办法用PHP做到这一点?

php string

65
推荐指数
6
解决办法
13万
查看次数

标签 统计

php ×1

string ×1