views:

1648

answers:

9

I wouldn't know under what keyword to look for this in the PHP database, so I'm asking here.

Reason I want to know is because of how different Operating Systems handle new lines in textdocuments.

I'm using a CSV file in windows but each time I think I add a new line, what really happens is the new line gets pasted to the back of the latest line.

Reason is, in windows, a new line is this: \r\n And the CSVHandler.class.php file I'm using only adds \n

However, in MAC OS X that's the new line, which is different from windows.

So I'm looking for this so I can implement a simple if() statement and solve this. Currently I've hardcoded the \r\n, but it should be simpler, no?

+2  A: 

Check the $_SERVER variable.

echo "<pre>";
print_r($_SERVER);

You can then use strstr (or any string comparison function) to check if you are on Windows. In this example, I checked the SERVER_SIGNATURE but you can use whatever key you want.

$isWindows = strstr($_SERVER[SERVER_SIGNATURE], "Win32") !== FALSE;
MrValdez
$_SERVER['SERVER_SIGNATURE'] was empty on my Windows machine (Apache 2.2 running PHP 5.3 on Windows 7 64 bits). I used another $_SERVER variable: $isWindows = strstr($_SERVER['SERVER_SOFTWARE'], "Win32") ? true : false;
bouke
+4  A: 

PHP has included the constant PHP_EOL for solving the problem you face, available since php 4.3.10 and PHP 5.0.2 - it contains a suitable end-of-line sequence for the server that PHP is running on.

If you want to use a different end-of-line sequence suitable for a particular client, then you'll have to code that yourself. One way to determine the client OS is to use get_browser, assuming your server has an up-to-date browscap.ini

Paul Dixon
A: 

Also, try this function:

$b = get_browser(null, true);

and in $b['platform'] will be OS.

BTW, *nix OS use \n as new line. Mac usees \r, Windows - \r\n

Sergei
We're looking for the server, not the browser.
stalepretzel
get_browser also gets the OS
Paolo Bergantino
stalepretzel, just see the docs.
Sergei
+1  A: 

You may also want to do a php info call to have a look at a lot of the configuration settings on your PHP setup, code is simple:

phpinfo();
Andrew G. Johnson
A: 

Probably the safest thing to do when reading is to determine the line ending character(s) from the file itself, or accept all line endings interchangeably. This protects you from harm if you copy the csv file from one machine to another with a different OS. If you read before writing, you can make your output line endings match the line endings you identified when reading.

For CSV files, php has some library functions. Try searching php.net for fgetcsv and fputcsv. There is the auto_detect_line_endings which can be set in the php.ini, but I don't know the specifics of how it works.

I always use the "\n" by itself on both linux and windows. I use notepad to edit them in windows and it doesn't break the endings. For my own use of csv I find that it's too much hassle to support different endings, but if it's something users have to interact with then you want to be safe rather than convenient.

Mnebuerquo
+4  A: 

*"BTW, nix OS use \n as new line. Mac usees \r, Windows - \r\n"

ARRRGH! PLEASE STOP PERPETUATING THIS MYTH!

Mac OS 9 used that like 10 years ago, but no one uses OS9 anymore. MACS USE UNIX LINE ENDINGS. \n. "Mac" used today should refer to contemporary computers, just as "Windows" refers to XP or vista unless otherwise qualified.

Saying Macs use \r is about as correct as saying that "Windows runs on top of MS-DOS, supports only the FAT16 filesystem, and has no 64-bit support."

Nobody should ever ever use \r for anything under any circumstances. Unless they are targeting old-ass macs.

+3  A: 

The information about the server operating system can be obtained with php_uname() function:

echo 'I have been run on '.php_uname('s');

It also allows to retrieve the full information on the version.

Zyx
Helped me out with a similar question. +1
MitMaro
A: 

'Win16', 'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)', 'Windows 98' => '(Windows 98)|(Win98)', 'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)', 'Windows XP' => '(Windows NT 5.1)|(Windows XP)', 'Windows Server 2003' => '(Windows NT 5.2)', 'Windows Vista' => '(Windows NT 6.0)', 'Windows 7' => '(Windows NT 7.0)', 'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)', 'Windows ME' => 'Windows ME', 'Open BSD' => 'OpenBSD', 'Sun OS' => 'SunOS', 'Linux' => '(Linux)|(X11)', 'Mac OS' => '(Mac_PowerPC)|(Macintosh)', 'QNX' => 'QNX', 'BeOS' => 'BeOS', 'OS/2' => 'OS/2', 'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)' );

// Loop through the array of user agents and matching operating systems foreach($OSList as $CurrOS=>$Match) { // Find a match if (eregi($Match, $_SERVER['HTTP_USER_AGENT'])) { // We found the correct match break; } } // You are using Windows Vista echo "You are using ".$CurrOS; ?>

ok okok okok okok okok okok okperuvian

fernando
A: 

$svr_os=strtolower(reset(explode(' ',php_uname('s'))));

$isLinux=$svr_os==='linux';

$isWindows=$svr_os==='windows';

josé