str_split sin ajuste de palabra

Estoy buscando el fastest solution, a un 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");

preguntado el 10 de marzo de 12 a las 16:03

1 Respuestas

You actually can use wordwrap(), fed into explode(), using the newline character \n como delimitador. 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"
}

respondido 07 mar '14, 20:03

Note: using false (default) as the 4th param prevents words from being broken when wrapping. Exactly what I needed. If you don't care about breaking words, set this to true. - Ryan

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.