I've been assigned a project that have associative arrays without single quotes all over the place.
Which should be:
This generates a PHP notice and it's very bad practice.
Quoting from <a href="http://www.php.net/manual/en/language.types.array.php" rel="nofollow">PHP.net</a>:<blockquote>
This is wrong, but it works. The reason is that this code has an undefined constant (bar) rather than a string ('bar' - notice the quotes). PHP may in future define constants which, unfortunately for such code, have the same name. It works because PHP automatically converts a bare string (an unquoted string which does not correspond to any known symbol) into a string which contains the bare string. For instance, if there is no defined constant named bar, then PHP will substitute in the string 'bar' and use that.</blockquote>
And it's not good for performance because it has to search for a constant, before using
So I was wondering if someone knows a script that can achieve this without having to do it manually in a one by one basis for every file.
I use Vim as an editor, and I know how to use its search and replace command in a single file, however I see two problems with this:<ol><li>
I can't search and replace something like this because I don't think it supports regular expressions.</li> <li>
It only works in the current opened file (as far as I know).</li> </ol>Answer1:
Does your text editor not support global search and replace using regular expressions?
I might search for something like:
and replace with
Regular expression search+replace is really useful for broad changes to lots of source files. Although make sure you take a snapshot of your code before you make the global change and verify afterwards that everything you wanted changed was changed with no unintended side-effects.
Based on OP's update that s/he is using vim, see the accepted answer of this post: <a href="https://stackoverflow.com/questions/3225477/apply-regular-expression-substitution-globally-to-many-files-with-a-script" rel="nofollow">Apply regular expression substitution globally to many files with a script</a> which describes using regular expression search/replace on multiple files in vim.
The sed-like syntax suggests to me that the command you are looking for (untested) is:
:args **/*.php | argdo %s/(\$[a-zA-Z0-9]+\[)([^\]]+)\]/\1'\2'/ge | update
You might need to fiddle with that a bit, so make a backup before you go modifying all your files. I recommend trying to find a more user-friendly IDE as well.Answer2:
Does you IDE do a search and replace? If so use that. It might take some time but you need to be careful with just changing all of them in one go.Answer3:
This would be another possible solution with vim:
This would change all instances of:
And will prompt for confirmation.
The following examples would remain unchanged (as they should be):
[$foo] ["foo"] ['foo'] Answer4:
Use an IDE to do a find and replace on the project. Most of them allow you to put a regex in the search string, so whenever it matches the pattern replace