如何修复多个文件中缺少单引号的关联数组键 [英] How to fix associative array keys that lack single quotation marks in multiple files

查看:84
本文介绍了如何修复多个文件中缺少单引号的关联数组键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经分配了一个项目,该项目具有到处都是单引号的关联数组.

I've been assigned a project that have associative arrays without single quotes all over the place.

示例:

$foo[bar]

应该是:

$foo['bar']

这会生成PHP通知,这是非常不好的做法.

This generates a PHP notice and it's very bad practice.

PHP.net 引用:

这是错误的,但是可以.原因是此代码具有未定义的常量(bar),而不是字符串("bar"-请注意引号).PHP将来可能会定义常量,不幸的是这样的代码,具有相同的名称.之所以有效,是因为PHP自动运行转换一个裸字符串(一个不带引号的字符串,该字符串不对应到任何已知符号)到包含裸字符串的字符串中.为了实例,如果没有定义的名为bar的常量,则PHP将替换字符串"bar"并使用它.

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.

这对性能不利,因为它必须在使用'key'

And it's not good for performance because it has to search for a constant, before using 'key'

所以我想知道是否有人知道一个脚本可以实现此目的,而不必为每个文件手动逐个地进行操作.

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.

更新:

我使用Vim作为编辑器,我知道如何在单个文件中使用它的搜索和替换命令,但是我看到了两个问题:

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:

  1. 我无法搜索和替换这样的内容,因为我认为它不支持正则表达式.

  1. I can't search and replace something like this because I don't think it supports regular expressions.

它仅在当前打开的文件中有效(据我所知).

It only works in the current opened file (as far as I know).

推荐答案

您的文本编辑器是否不支持全局搜索和使用正则表达式替换?

Does your text editor not support global search and replace using regular expressions?

我可能会搜索以下内容:

I might search for something like:

(\ $ [a-zA-Z0-9] + \ []([^ \]] +)\]

并替换为

\ 1'\ 2']

正则表达式search + replace对于对许多源文件进行广泛的更改非常有用.尽管请确保在进行全局更改之前先对代码进行快照,然后再确认要更改的所有内容都已更改,但没有意外的后果.

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.

更新:

基于他/她正在使用vim的OP更新,请参见此帖子的可接受答案:

Based on OP's update that s/he is using vim, see the accepted answer of this post: Apply regular expression substitution globally to many files with a script which describes using regular expression search/replace on multiple files in vim.

类似于sed的语法向我建议,您要查找的命令(未经测试)为:

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 |更新

您可能需要对此进行一些调整,因此在进行所有文件修改之前先进行备份.我建议也尝试找到更用户友好的IDE.

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.

这篇关于如何修复多个文件中缺少单引号的关联数组键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆