0

我有一个这样的字符串:

$contenuto = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut lobortis condimentum commodo. Duis dui dui, rutrum et lacinia sit amet, lacinia in nulla.";

我需要将它分解为一个限制为 20 个字符的子字符串数组,如下所示:

Array
(
    [0] => "Lorem ipsum dolor ";
    [1] => "sit amet, ";
    [2] => "consectetur ";
    [3] => "adipiscing elit. Ut ";
    [4] => "lobortis condimentum";
    [5] => "commodo. Duis dui ";
    [6] => "dui, rutrum et ";
    [7] => "lacinia sit amet, ";
    [8] => "lacinia in nulla.";
)

我怎样才能做到这一点?

4

1 回答 1

5
$string_parsed = wordwrap($contenuto,20,"|",false);
$array = explode("|",$string_parsed);

wordwrap函数允许我们以给定的字符数将长文本换成行(在本例中为 20),您可以自己指定 break char(我给出了“|”用于拆分),最后一个值“false”是含义如果达到字符限制,它不应该破坏单词。

于 2012-04-24T15:52:27.443 回答