Batch script to replace PHP short open tags with <?php

前端 未结 13 1878
轻奢々
轻奢々 2020-11-30 01:49

I have a large collection of php files written over the years and I need to properly replace all the short open tags into proper explicit open tags.

change \         


        
13条回答
  •  情书的邮戳
    2020-11-30 02:47

    My previous answer I just overwrote with sed wont work, sed is too weak for this sort of thing IMO.

    So I've whipped up a perl-script that should do the trick, its hopefully very user-editable.

    #!/usr/bin/perl 
    
    use strict;
    use warnings;
    
    use File::Find::Rule;
    use Carp;
    
    my @files = File::Find::Rule->file()->name('*.php')->in('/tmp/foo/bar');
    
    for my $file (@files) {
        rename $file, $file . '.orig';
        open my $output, '>', $file or Carp::croak("Write Error with $file $! $@ ");
        open my $input, '<', $file . '.orig'
          or Carp::croak("Read error with $file.orig $! $@");
    
        while ( my $line = <$input> ) {
            # Replace 

    But note, I haven't tested this on any real code, so It could go "Bang" .

    I would recommend you have your code revisioned ( wait, its already revisioned, right? .. right? ) and run your test-suite ( Don't tell me you don't have tests ! ) on the modified code, because you can't be certain its doing the right thing without a fully fledged FSM parser.

提交回复
热议问题