dtv55860 2012-05-19 01:47
浏览 63
已采纳

有谁知道在多个html文件中更换标签及其间的所有内容的最简单方法?

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?

  • 写回答

2条回答 默认 最新

  • douxing1850 2012-05-19 06:04
    关注

    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("
    ")
    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条)

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来