Created
August 21, 2012 01:39
-
-
Save darraghenright/3410422 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# First, create a test file with the three | |
# different line ending types LF, CR & CRLF | |
# | |
echo -en 'unix!\nclassic mac!\rdos!\r\n' > orig.txt | |
exit $? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Look out for Classic Mac (CR) line endings etc. | |
*/ | |
ini_set('auto_detect_line_endings', true); | |
/* | |
* Files | |
*/ | |
$orig = 'orig.txt'; | |
$edit = 'edit.txt'; | |
/* | |
* Regex with perl - replace Unix LF with CR | |
* using PHP backticks - same as shell_exec() | |
* | |
* Write to a clean output file, or add -i for | |
* edit-in-place if you're feeling confident :) | |
*/ | |
`perl -pe 's/\n/\r/g' $orig > $edit`; | |
/* | |
* Read normalised file contents into array from file $edit | |
* Strip newlines from each element. | |
* Skip empty lines, which will occur when we convert a DOS CRLF into a CRCR ;) | |
*/ | |
$lines = file($edit, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); | |
/* | |
* Ta-dah! | |
*/ | |
var_dump($lines); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
array(3) { | |
[0]=> | |
string(5) "unix!" | |
[1]=> | |
string(12) "classic mac!" | |
[2]=> | |
string(4) "dos!" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: useful commands to view non-printing characters:
cat -v orig.txt
od -c orig.txt
file orig.txt