At my company we have the code convention to write $foo . $bar (with whitespaces). Because I couldn't get familiar with that (I don't want to discuss :P ), I wrote the following vim-replace-function:
:%s/\(\w\|\]\|'\|\"\)\s\?\.\s\?\('\|\$\|\"\|(\)/\1 \. \2/eg
..which matches everything I needed so far. But now I also have String which I don't want to replace.
So how can I ignore Strings like "foo.bar" or 'foo.bar', but not $foo.$bar ?
Update: I would also be happy with a completely scripted function. I just wonder if there is no other way than temporarily remove all php-strings. Re-calculating the actual replacement-positions would be a mess, no?
You can make use of syntax highlighting here:
:%s/\(\w\|\]\|'\|\"\)\s\?\.\s\?\('\|\$\|\"\|(\)/\=synIDattr(synID(line('.'),col('.'), 0),'name')!~?'phpString\(Single\|Double\)'?submatch(1) . ' . ' . submatch(2):submatch(1).'.'.submatch(2)/eg
Basically that means, if the string below the cursor is not of Syntax type phpStringSingle and not phpStringDouble, then replace by adding a ' . ' between each capturing group, else leave it as it was.
Read more about it at:
:h sub-replace-expression
:h expr1