php - Split string by white-space -
How can I divide a string from white space, it is not known how long the white space is?
For example, with the following string:
"Quick brown fox jumps on lazy dogs" I will get an array
['The', 'Quick', 'Brown', 'Fox', 'Jumps', 'Over', 'D', 'Lazy', 'Dog'];
You can use regular expressions to do this easily:
$ string = 'Quick brown fox jumps over lazy dogs'; $ Word = preg_split ('/ \ s + /', $ string, -1, PREG_SPLIT_NO_EMPTY); Print_r (word $); This produces this output:
array ([0] => [1] => immediately [2] = & gt; Brown [3] => Fox [4] = & gt; Jump [5] => Over [6] => [7] => Lazy [8] = & gt; Dog)
Comments
Post a Comment