mate64 mate64 - 2 years ago 140
PHP Question

str_split without word-wrap

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

$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");

Answer Source

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

array(4) {
  string(9) "The quick"
  string(9) "brown fox"
  string(10) "jumps over"
  string(12) "the lazy dog"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download