gpt4 book ai didi

php - 没有自动换行的 str_split

转载 作者:可可西里 更新时间:2023-11-01 13:06:00 26 4
gpt4 key购买 nike

我正在寻找最快的解决方案,到一个字符串分成几部分,没有 .

$strText = "The quick brown fox jumps over the lazy dog";

$arrSplit = str_split($strText, 12);

// result: array("The quick br","own fox jump","s over the l","azy dog");
// better: array("The quick","brown fox","jumps over the","lazy dog");

最佳答案

你实际上可以使用 wordwrap() , 被送入 explode(),使用换行符 \n 作为分隔符。 explode() 将在 wordwrap() 生成的换行符处拆分字符串。

$strText = "The quick brown fox jumps over the lazy dog";

// Wrap lines limited to 12 characters and break
// them into an array
$lines = explode("\n", wordwrap($strText, 12, "\n"));

var_dump($lines);
array(4) {
[0]=>
string(9) "The quick"
[1]=>
string(9) "brown fox"
[2]=>
string(10) "jumps over"
[3]=>
string(12) "the lazy dog"
}

关于php - 没有自动换行的 str_split,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9648019/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com