【问题标题】:Removing PHP Lines from an HTML File Using Python使用 Python 从 HTML 文件中删除 PHP 行
【发布时间】:2014-01-26 06:28:56
【问题描述】:

有人要求我从 html 文件中删除 PHP。我相信我可以用 Python 自动化这个过程,但我被困在多行 PHP 代码上。下面是 PHP 代码示例。

<?php 
  $seg = $this->uri->segment(2);
  $active_2 = '';$active_1 = '';$active_4 = ''; $active_3 = '';
  if($seg == "Enrichment"){
      $active_1 = 'class="active"';
  }
  elseif($seg == "Nightlife"){
     $active_2 = 'class="active"'; 
  }
  elseif($seg == "Misc"){
     $active_3 = 'class="active"'; 
  }
  else $active_4 = 'class="active"';
?>
<a class="" href="<?php echo base_url()?>"><div class="logo">Page Name</div></a>
<li><a href="<?php echo base_url()?>category/all" <?php echo $active_4?> onClick="_gaq.push(['_trackEvent','categories','All'])">All</a>

这是我的代码。如您所见,我没有处理多行事件。我已经设法自己提取了所有其他 PHP 事件,所以我已经为我准备好了。

with open('C:/Users/B/Documents/Python(s)/aaa_phpshit/top_header.php', 'r') as php_file:
for line in php_file:
    while line.find("<?php") > 0: 
        if "<?php" in line.lower() and "?>" in line:
            line = line.replace(line[line.find("<?php"):line.find("?>")+len("?>")].strip(), "")
        else:
            break
    print line

我尝试了几种不同的方法来解决多线问题,但还没有任何效果。任何建议或见解将不胜感激。

【问题讨论】:

  • 为什么不在列表/字典中保留 ""(不管是哪一行)?像一条记录:{Xnth:{'start':posx,'end':posz}}
  • 正则表达式替换&lt;\?php.*?\?&gt;
  • @hjpotter92 我希望使用纯 python,因为 (a) 我想学习 (b) 我真的很不擅长正则表达式(总是有一些极端情况我不明白 b/c 正则表达式不介意)——甚至比我在 python 做的还要多。此外,即使使用正则表达式,多行出现仍然需要我放弃逐行循环,对吗?我只需要 var_name = open(doc).read()??

标签: python regex python-2.7 syntax


【解决方案1】:

最好的方法是使用RE


示例:

import re

with open('php_file.txt', 'r') as f:
    html = re.sub('<\?.*?\?>', '', f.read(), re.MULTILINE)

with open('html_file.txt', 'w') as f:
    f.write(html)

【讨论】:

    【解决方案2】:

    不是最有效的例子,但给你一个开始:

    k = 0
    x = 0
    ln = len(html)
    ret = ''
    while True:
        if html[x:x + 5] == '<?php':
            ret = '%s%s' % (ret, html[k:x])
            k = x
            x += 4
        if html[x:x + 2] == '?>':
            k = x + 2
    
        x += 1
        if x >= ln:
            ret = '%s%s' % (ret, html[k:])
            break
    
    print(ret)
    

    显然,您的源代码是 html var。请注意,不检查错误:如果您忘记关闭 php 标签,结果与您的 html/php 源代码一样错误

    【讨论】:

      【解决方案3】:

      您不必逐行执行。正则表达式真的很值得研究:)

      import re
      
      php_file = open(filename, 'r').read()
      html = re.sub('<\?.*?\?>', '', php_file, re.MULTILINE)
      

      请注意正则表达式中的lazy syntax(即非贪婪)

      【讨论】:

        【解决方案4】:

        这是我用来完成任务的确切代码。感谢 SO 用户的帮助。希望这个问题/代码有一天可以帮助其他人。

        import os
        import re
        dir_list = ['views/templates','views/pages']
        for dir in dir_list:
        for file in os.listdir('C:/Users/B/Documents/Python(s)/application/'+dir):
            if file.endswith(".php"):
                with open('C:/Users/B/Documents/Python(s)/application/'+dir+'/' + file, 'r') as f:
                    html = f.read()
                    php_compile = re.compile("<\?.*?\?>", re.DOTALL)
                    html = re.sub(php_compile, '', html)
                with open('C:/Users/B/Documents/Python(s)/application/'+dir+'/' + file, 'w') as f:
                    f.write(html)
            else:
                print "dir name: ", dir
                print "\nfile name: ", file, "\n"
        

        【讨论】:

          猜你喜欢
          • 2010-12-10
          • 1970-01-01
          • 1970-01-01
          • 2014-12-14
          • 2012-12-23
          • 1970-01-01
          • 2016-01-20
          • 1970-01-01
          • 2013-09-01
          相关资源
          最近更新 更多