I am wanting to update my php script as a project (globally) and change array(elements); to [elements];
Using atom i have already ran the update for just the init of arrays and have already changed array(); to []; however this just takes care of the empty array initialization codes.
The challenge now is to change arrays that actually have elements in them from () to []
For example the arrays could be in several formats
array('content',number content,'content');
or
array( 'content', array( number content,'content' ) );
or any variation of the number of levels.
I have broken it down this way but i dont know how to do the regex.,
Find every occurance of array( – replace with [
Find the associated close of that array ); -replace with ];
Find every array( inside another which is array( -replace with [
Find the associated close of each of those arrays that are inside another array ); or just ), – replace with ]; or ],
I think that should cover it…. is the regex going to be impossible to do?
thanks
Advertisement
Answer
Seeing, that you are willing to do some manual repairs afterwards and that the solution suggested in my comment is of interest to you I now put it in “proper” answer form. This is easier to edit and to discuss. As said before, this is only a “workaround”, as we are not really parsing the text in the sense of really understanding it syntactically. However, when you apply the following Regexp
barray(([^()]*))
repeatedly and replace it with
[$1]
(yes, I left out the “array” in front of “[“!) you might have a chance of doing what you want.
I put a b
in front of the Regexp to make it look for a word boundary. This way we exclude text fragments like is_array()
or myarray()
from being changed.