Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

str_split without word-wrap

I'm looking for the fastest solution, to split a string into parts, without word-wrap.

$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");
like image 236
mate64 Avatar asked Mar 10 '12 16:03

mate64


1 Answers

You actually can use wordwrap(), fed into explode(), using the newline character \n as the delimiter. explode() will split the string on newlines produced by 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"
}
like image 193
Michael Berkowski Avatar answered Oct 29 '22 21:10

Michael Berkowski