当前位置: 动力学知识库 > 问答 > 编程问答 >

php - how to convert a string in array respect the spaces

问题描述:

I have a string like this:

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

I need to break it into an array of substrings with a limit of 20 characters, like this:

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.";

)

How can I do this?

网友答案:
$string_parsed = wordwrap($contenuto,20,"|",false);
$array = explode("|",$string_parsed);

wordwrap function allows us to wrap long text into lines at given character counts (in this case it is 20), you can specify the break char yourself (I've given "|" for splitting), and the last value "false" is meaning that it shouldn't break the word if it reaches the character limit.

分享给朋友:
您可能感兴趣的文章:
随机阅读: