Skip to content

Instantly share code, notes, and snippets.

@darraghenright
Created August 21, 2012 01:39
Show Gist options
  • Save darraghenright/3410422 to your computer and use it in GitHub Desktop.
Save darraghenright/3410422 to your computer and use it in GitHub Desktop.
#!/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 $?
<?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);
array(3) {
[0]=>
string(5) "unix!"
[1]=>
string(12) "classic mac!"
[2]=>
string(4) "dos!"
}
@darraghenright
Copy link
Author

Note: useful commands to view non-printing characters:

cat -v orig.txt
od -c orig.txt
file orig.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment