Preserve newlines when using php split
I'm using p开发者_Python百科hp's split function to break a pipe delimited record into an array
explode("|",$line)
When there is a newline within the pipe field, split puts it into a new array field
blue|is my favorite|color
works fine and gives 3 array elements
blue|is my
favorite|color
results in 4 array elements
How do I make it retain newlines?
Splitting a string on |
using explode()
will not affect newlines.
The only way you will get 4 elements in the resulting array is if there is a |
at the start or end, which result in an array member with a blank string. You can drop these with array_shift()
and array_pop()
respectively.
精彩评论