2015-12-05 45 views
1

工作假设$text = ea\r\ndad\r\n\r\nedadePHP的换行符爆炸完全不

我的第一个代码:

$text = explode("\r\n",$text); 

我的新代码

function splitNewLine($text) { 
    $code=preg_replace('/\n$/','',preg_replace('/^\n/','',preg_replace('/[\r\n]+/',"\n",$text))); 
    return explode("\n",$code); 
} 
$text = splitNewLine($text); 

在这两种情况下,$text结束了这样的:

Array 
(
    [0] => ea\r\ndad\r\n\r\nedade 
) 

我真的不知道为什么......这是关于它,没有更多的代码缺失,但它不会工作。任何想法为什么发生这种情况?

+1

'使preg_split( '/ \ r吗?\ n /',$海峡, -1,PREG_SPLIT_NO_EMPTY);' –

+0

@ SverriM.Olsen不,同样。这很奇怪AF – Fane

+0

向我们展示您正在使用的代码。 http://sandbox.onlinephpfunctions.com/code/e013edc2b051715b03fa60b8ebc2fb108d787b63 –

回答

1

最好是使用PHP_EOL

$temp = explode(PHP_EOL, $text); 

OR

$temp = preg_split('/\r\n|\r|\n/', $text); 

希望它可以帮助你

+0

'PHP_EOL'取决于平台。如果数据来自其他地方,那么它可能无法按预期工作。 –