将字符串分成多行

时间:2015-06-02 16:52:15

标签: php

我需要根据特定的字符串长度将句子/字符串拆分成小块。 PHP的str_split函数正在完成一半的工作;我需要保留一个完整的词。换句话说,我不希望在每一行的末尾突然将一个单词分成两个单词。我无法弄清楚解决方案。如果你建议我的话会很有帮助。

到目前为止,这是代码和结果。

$line = "After six months, participants in both vitamin D supplementation groups had lost more weight and had greater reductions in their waistlines than those who hadn't taken the supplements, Vigna's team said";
$chunks = str_split($line, 35);
echo '<pre>' . print_r($chunks, true);




Array
(
    [0] => After six months, participants in b
    [1] => oth vitamin D supplementation group
    [2] => s had lost more weight and had grea
    [3] => ter reductions in their waistlines 
    [4] => than those who hadn't taken the sup
    [5] => plements, Vigna's team said
)

0 个答案:

没有答案