Regular expression to update the PHP tag

Many moons ago, PHP used <? to denote the start of a php script.  This caused a problem with some XML code, so it was decided to use <?php instead.  For some time, PHP no longer recognises <?, although there is an option short_open_tag=On to turn them back on. I recently had to copy some scripts to a new server and I decided to take the ‘bull by the horns’ and update the tags (rather than turn the tag option back on). I worked out the following regular expression to allow me to replace them all in bulk. My editor allows a search and replace by regular expression.

Search: “<\?([^p])”

Replace: “<?php \1″

(make sure you do not have spaces before and after the expressions, but note the space within the replace expression.

previous entry
next entry

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.