我需要将所有$arr[key]
替换为$arr['key']
,而不是$arr[$key]
或$arr[CONST]
或已经重写为$arr['key']
最终$arr[key]
{$arr['key']}
谢谢!
答案 0 :(得分:2)
您可以尝试以下方式:
perl -pe 's/(\$\w+\[\s*)(?![A-Z\d]+\b)(\w+)(\s*])/$1\'$2\'$3/g' file.php
示例:
echo $arr[foo] $arr['ok'] $arr[3] $arr[CAPS] $arr[$var] $arr[ bar ] | perl -pe "s/(\$\w+\[\s*)(?![A-Z\d]+\b)(\w+)(\s*])/$1'$2'$3/g"
$arr['foo'] $arr['ok'] $arr[3] $arr[CAPS] $arr[$var] $arr[ 'bar' ]