Skip to content

Instantly share code, notes, and snippets.

@daybreaker
Created October 7, 2010 22:09
Show Gist options
  • Save daybreaker/616001 to your computer and use it in GitHub Desktop.
Save daybreaker/616001 to your computer and use it in GitHub Desktop.
# 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