Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

explode error \r\n and \n in windows and linux server

I have used explode function to get textarea's contain into array based on line. When I run this code in my localhost (WAMPserver 2.1) It work perfectly with this code :

$arr=explode("\r\n",$getdata);

When I upload to my linux server I need to change above code everytime into :

$arr=explode("\n",$getdata);

What will be the permanent solution to me. Which common code will work for me for both server?

Thank you

like image 377
Aryan G Avatar asked Dec 10 '22 09:12

Aryan G


1 Answers

The constant PHP_EOL contains the platform-dependent linefeed, so you can try this:

$arr = explode(PHP_EOL, $getdata);

But even better is to normalize the text, because you never know what OS your visitors uses. This is one way to normalize to only use \n as linefeed (but also see Alex's answer, since his regex will handle all types of linefeeds):

$getdata = str_replace("\r\n", "\n", $getdata);
$arr = explode("\n", $getdata);
like image 178
Emil Vikström Avatar answered Dec 15 '22 00:12

Emil Vikström