Thursday, 6 May 2010

finding <? that aren't <?php

I'm using this pattern (in vim) to find "<?" that aren't "<?php":

\(<?\)\@<=\(php\)\@!

it uses zero-width look behind for the "<?" and zero width negative look ahead for the "php".

And using grep (in perl mode):

grep -P "\<\?(?!php)" -r .


Test cases:

YES <?
NO <?php
YES <?=
YES <? asdf
YES <?adsf
NO asdg



And a quick shell script for fixing a file (I say "quick" because it doesn't always work - e.g. if you have "<?/* comment */?>" it doesn't put in the extra whitespace you need after the "<?php"):

sed 's/<?=/MAGICaaxo3ohW/' | sed 's/<?php/<?/g' | sed 's/<?/<?php/g' | sed 's/MAGICaaxo3ohW/<?php echo/'

No comments:

Post a Comment