开发者

Preserve newlines when using php split

开发者 https://www.devze.com 2023-02-25 11:04 出处:网络
I\'m using p开发者_Python百科hp\'s split function to break a pipe delimited record into an array

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消