Recently, I have begun to replace the use of PHP "short tags" (e.g. using "<?" and "<?=" in PHP code instead of the standard tags "<?php" and <?php echo ...").
To make this easier, I've developed the following regular expression substitution commands in VIM to automatically find and replace the PHP short tags with standard ones.
Note that since there are two ways that the PHP short tags are used in PHP code: the first to indicate a block of code, and the second to just echo a single variable (<?=$foo?>, that I use two separate substitutions. Each engineered so as to not make things harder for the other.
0. Find (recursively) all files in the current directory using PHP "short tags"
Code:
grep -rl "<?[^p]" *
1. Replace "<?" at the beginning of code blocks with "<?php."Code:
%s/<?\([ \t\r\n]*[^a-zA-Z=]\)/<?php\1/gc
This substitution doesn't currently find all the opening short tags... Still working on it.
Notes on the expression:
"
[ \t\r\n]*" matches any whitespace characters
"
[^a-zA-Z=]" excludes any alpha characters or equal signs immediately following the "<?". This will avoid replacing properly written standard tags "<?php", and munging tags intended to be replaced by the "<?=$foo?> fixing substitution.
2. replace "<?=$foo?>" with "<?php echo $foo;?>."Code:
%s/<?=\([0-9a-zA-Z_\'\"\(\)\[\]\$,]*\)?>/<?php echo \1;?>/gc
Notes on the expression: the strings we're trying to substitute can contain any of the following characters, so we have to include them in the expression: "
0-9 a-z A-Z _ " ( ) [ ] $ ,".