可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
回答1:
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
回答2:
*"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:
You could use the predefined constant PHP_OS.
I'm using
if (PHP_OS === 'WINNT') {...}
回答4:
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.
回答5:
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;
回答6:
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();
回答7:
As far as I know, if PHP is running on Windows, there will be an environment variable named WINDIR. I think that you could do this:
$isWindows = isset($_SERVER['WINDIR']);
回答8:
Lots of answers aleady, but here is my 2cents:
function windows_server()
# Purpose: Check if server is Windows
{
return in_array(strtolower(PHP_OS), array("win32", "windows", "winnt"));
}
## --------------------------------------------------------
function linux_server()
# Purpose: Check if server is Linux
{
return in_array(strtolower(PHP_OS), array("linux", "superior operating system"));
}
回答9:
I'd recommend php_uname. Just read the explanation from the docs:
php_uname — Returns information about the operating system PHP is
running on
I did like the suggestion of PHP_OS above, but be careful, per this note from the docs:
consider using the PHP_OS constant, but keep in mind this constant
will contain the operating system PHP was built on.
回答10:
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.
回答11:
$svr_os=strtolower(reset(explode(' ',php_uname('s'))));
$isLinux=$svr_os==='linux';
$isWindows=$svr_os==='windows';
回答12:
<?php
$OSList = array
(
// Match user agent string with operating systems
'Windows 3.11' => '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;
?>
回答13:
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