Detect EOL type using PHP
/**
* Detects the end-of-line character of a string.
* @param string $str The string to check.
* @param string $default Default EOL (if not detected).
* @return string The detected EOL, or default one.
*/
function detectEol($str, $default=''){
static $eols = array(
"\0x000D000A", // [UNICODE] CR+LF: CR (U+000D) followed by LF (U+000A)
"\0x000A", // [UNICODE] LF: Line Feed, U+000A
"\0x000B", // [UNICODE] VT: Vertical Tab, U+000B
"\0x000C", // [UNICODE] FF: Form Feed, U+000C
"\0x000D", // [UNICODE] CR: Carriage Return, U+000D
"\0x0085", // [UNICODE] NEL: Next Line, U+0085
"\0x2028", // [UNICODE] LS: Line Separator, U+2028
"\0x2029", // [UNICODE] PS: Paragraph Separator, U+2029
"\0x0D0A", // [ASCII] CR+LF: Windows, TOPS-10, RT-11, CP/M, MP/M, DOS, Atari TOS, OS/2, Symbian OS, Palm OS
"\0x0A0D", // [ASCII] LF+CR: BBC Acorn, RISC OS spooled text output.
"\0x0A", // [ASCII] LF: Multics, Unix, Unix-like, BeOS, Amiga, RISC OS
"\0x0D", // [ASCII] CR: Commodore 8-bit, BBC Acorn, TRS-80, Apple II, Mac OS <=v9, OS-9
"\0x1E", // [ASCII] RS: QNX (pre-POSIX)
//"\0x76", // [?????] NEWLINE: ZX80, ZX81 [DEPRECATED]
"\0x15", // [EBCDEIC] NEL: OS/390, OS/400
);
$cur_cnt = 0;
$cur_eol = $default;
foreach($eols as $eol){
if(($count = substr_count($str, $eol)) > $cur_cnt){
$cur_cnt = $count;
$cur_eol = $eol;
}
}
return $cur_eol;
}
Notes:- Needs to check encoding type
Needs to somehow know that we may be on an exotic system like ZX8x (since ASCII x76 is a regular letter)@radu raised a good point, in my case, it's not worth the effort to handle ZX8x systems nicely.- Should I split the function into two?
mb_detect_eol()
(multibyte) anddetect_eol()
How to detect if a string has a new line break in it?
Your existing test doesn't work because you don't use double-quotes around your line break character ('\n'
). Change it to:
if(strstr($string, "\n")) {
Or, if you want cross-operating system compatibility:
if(strstr($string, PHP_EOL)) {
Also note that strpos
will return 0 and your statement will evaluate to FALSE if the first character is \n
, so strstr
is a better choice. Alternatively you could change the strpos
usage to:
if(strpos($string, "\n") !== FALSE) {
echo 'New line break found';
}
else {
echo 'not found';
}
When do I use the PHP constant PHP_EOL?
Yes, PHP_EOL
is ostensibly used to find the newline character in a cross-platform-compatible way, so it handles DOS/Unix issues.
Note that PHP_EOL represents the endline character for the current system. For instance, it will not find a Windows endline when executed on a unix-like system.
PHP code is displayed incorrectly [ in one line ] EOL
Use an editor like Notepad++
for editing your code.
There are two possibilities. Godaddy server you are having is on Windows
and you are on a Unix based system. And the Other way around.
In short, \r\n
breaks used in UNIX is not actually compatible with \n
breaks used in Windows. So use an editor like Notepad++ which has a preference to save with \r\n
breaks by default.
Edit:
Try switching the transfer mode from Binary to ASCII as well in the FTP program you are using. Usually FTP softwares are supposed to cross support the text files on the host and client during the transfer.
Edit 2[ Thanks @Wesley Murch ]
For easy switching of EOL(End of Line) Format : Edit -> EOL Convertion -> Windows
Format
End of line character is invalid
I assume you are using windows.
Your file uses unix lineendings ("\n")
Windows uses ("\r\n")
you could convert the lineendings in the commandline with
type input_filename | more /P > output_filename
or open your sourcefiles with an editor like notepad++ and change lineendings to windows
Related Topics
Save Accents in MySQL Database
Deny Direct Download of File Using PHP
When Do/Should I Use _Construct(), _Get(), _Set(), and _Call() in PHP
Converting Float Decimal to Fraction
How to Avoid Request Entity Too Large 413 Error
How to Access My Wamp Server on Another Local Computer
I Want to Find Current Location of User in PHP
SQL Select * from Multiple Tables
.Htaccess Rewrite: Subdomain as Get Var and Path as Get Var
How Would You Transform a Pre-Existing Web App into a Multilingual One
Add Some Attribute Values to Woocommerce Variable Product Title from Chosen Variation
Parsing Xml Data Using PHP to Put into MySQL Database
How to Extract Frames of an Animated Gif with PHP
Encoding Byte Data into Digits
How to Add/Set Images on PHPoffice/Phpword Template
What Is an Appropriate Content-Type Header for JavaScript Files