0

I have a lot of html files with custom style sheets within. I would like to create one universal style sheet that they all can use. So what i need to do is replace

< style type="text/css">

.Title {.... .body {..

< /style>

including the tags with this in multiple html files.

< link media="screen" href="style.css" type="text/css" rel="stylesheet" />

Im sure someone has ran into this problem and im wondering what would be the easiest way to accomplish this task? is there some kind of applescript or php script i can run to do this or is there another way of doing this?

1

2 Answers 2

1

I'd usually just use TextMate's Search in Project or some Ruby or shell script. I have a script like this saved as ggsub.rb and just modify it when I need to replace something in multiple files.

#!/usr/bin/env ruby -Ku

# paths = `find ~/Sites -name '*.html'`.chomp.split("\n")
paths = Dir["*.html"]

paths.each do |path|
  html = File.read(path)
  html.sub!(/<style.*?>.*?<\/style>/m,
  "<link rel=\"stylesheet\" href=\"style.css\">")
  puts html
  # File.open(path, "w") { |f| f.puts(html) }
end
1
  • This worked. Just uncomented the File.open and worked like a charm.
    – Mike
    May 19, 2012 at 8:19
1

See the accepted answer to this question, posted earlier today.

How to remove entire div with preg_replace

It's very close to what you're trying to do. In short, use the DOMDocument class - http://www.php.net/manual/en/class.domdocument.php

1
  • Thanks for the reference. used the ruby script instead and it worked like a charm. Did not activate apache on my mac yet.
    – Mike
    May 19, 2012 at 8:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.