Skip to content

Instantly share code, notes, and snippets.

@JPry
Last active December 31, 2015 18:10
Show Gist options
  • Save JPry/f7b6179e8b583f3a71b5 to your computer and use it in GitHub Desktop.
Save JPry/f7b6179e8b583f3a71b5 to your computer and use it in GitHub Desktop.
Remove BOM from a given file. Includes error checking.
<?php
/**
* A simple script to remove BOM from a given file.
*
* @author Jeremy Pry
*/
if ( $argc < 2 ) {
echo "Please include a file to operate on!" . PHP_EOL;
die( 1 );
}
$file = $argv[1];
if ( ! file_exists( $file ) ) {
echo "File [{$file}] does not exist!" . PHP_EOL;
die( 2 );
}
if ( ! is_writable( $file ) ) {
echo "Cannot write to file [{$file}]" . PHP_EOL;
die( 3 );
}
if ( is_dir( $file ) ) {
echo "[{$file}] is a directory!" . PHP_EOL;
die(4);
}
file_put_contents( $file, str_replace( "\xef\xbb\xbf", '', file_get_contents( $file ) ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment