一尘不染

从字符串中获取前100个字符,并注意完整单词

php

我之前在这里问过类似的问题,但是我需要知道这种小的调整是否可能。我想将字符串缩短为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做到这一点?


阅读 295

收藏
2020-05-26

共1个答案

一尘不染

您需要做的就是使用:

$pos=strpos($content, ' ', 200);
substr($content,0,$pos );
2020-05-26