Created
October 7, 2010 22:09
-
-
Save daybreaker/616001 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
# Needed a script to loop through several php files modify any links, and insert some code into the header of each page. | |
# Since the header was standard on each page, it was easy enough to match, as were any links | |
# My question: How could I refactor this to be more efficient than using two different sub/gsubs, and a seperate file write? | |
Dir.glob('*.php') do |file_name| | |
#Find one instance of the header, and modify it | |
content = File.read(file_name).sub(/regex/i, "multiline\nreplacement") | |
#find every instance of <a href="blah.php"> and modify it to say <a href="blah.php?foo=bar"> | |
content.gsub!(/regex/i,'replacement') | |
#write the modified contents to a new file with the same name | |
file = File.open(file_name, 'w') { |file| file.puts content } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment