Created
September 23, 2013 07:41
-
-
Save littlehaker/6667527 to your computer and use it in GitHub Desktop.
去除UTF8文件开头的BOM信息
This file contains hidden or 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
#!/usr/bin/env ruby -w | |
# -*- coding: UTF-8 -*- | |
# 去除UTF8文件开头的BOM信息 | |
# usage: cat file | ruby no-UTF8-BOM.rb | |
# or: ruby no-UTF8-BOM.rb file | |
content = '' | |
if ARGV.length > 0 then | |
# 参数模式 | |
File.open(ARGV[0], "r") do |file| | |
content = file.read.force_encoding("UTF-8") | |
end | |
else | |
# 管道模式 | |
content = STDIN.read.force_encoding("UTF-8") | |
end | |
puts content.gsub!("\xEF\xBB\xBF".force_encoding("UTF-8"), '') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment