Regular expression to move legacy array index PHP code to PHP 5.4.x

This was a bit of  headache; after we upgraded our servers to use php 5.4 we had a few clients who’s sites that went down.  For the most part it was a simple fix, of prepending a globalizer file to all page requests via htaccess, or just updating the session handling routines as various functions like session_register(), session_destroy().  However one of the sites was heavily relying on old style array syntax so things like unquoted associative array keys such as $var[index] (instead of the proper $var[“index”] were very pervasive in the system.  There were thousands of instances of this type of code, to replace them we ran the following regular expression find and replace method on the directory, we use the Netbeans which allows you to do a regular expression find and replace on a directory.

Here’s the regular expression that will find the unquoted array indices and replace them:

search string:  \$[a-zA-Z0-9]+\[)([a-zA-Z]+[0-9]*)\]

replace string: $1″$2″]




Leave a Reply

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