Jump to content

[SOLVED] Reliably determining what OS a php script is run on


jordanwb

Recommended Posts

I simply use this function

 

fix_endline( $string ) {
  return str_replace( array("\r\n", "\r"), "\n", $string );
}

 

Converts all possible 'alternate' endline combinations (\r\n = Windows, \r = OS9 ) to simply \n and is quite fast.

I need the info to determine line endings for writing files in PHP.

 

As far as I'm aware all operating systems use \n except windows which I believe is \n\r

 

Apparently Mac uses a solitary \r. But Mac OS X may use \n because Mac OS X is basically a unix based OS.

 

You can just use the PHP_EOL constant.

 

Now that's convenient.

 

I simply use this function

 

fix_endline( $string ) {
  return str_replace( array("\r\n", "\r"), "\n", $string );
}

 

Converts all possible 'alternate' endline combinations (\r\n = Windows, \r = OS9 ) to simply \n and is quite fast.

 

But that may not give you the right one. For Widnows having a solitary \n will screw up editing the file with notepad or whatever.

You can just use the PHP_EOL constant.

Now that's convenient.

 

Indeed it is.  And you can always put it into a variable if you want, or just do something like:

$string = "foo\nbar";

$fixed = str_replace("\n", PHP_EOL, $string);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.