duanputian5341 2013-12-09 12:25
浏览 57
已采纳

用于修改数百个文件中的php数组元素的脚本

I have some severely deprecated PHP code that I'm sifting through. One of the problems that I have is hundreds of errors like this:

[Mon Dec 09 07:00:33 2013] [error] [client 127.0.0.1] PHP Notice: Use of undefined constant id - assumed 'id' in /home/srv/site.local/content/online.php on line 171, referer: http://site.local/index.php

These result in the practice of a previous coder of calling arrays like this:

$array[some_element]

instead of:

$array['some_element']

So I know how to fix that by going through each file and adding the quotes, over, and over, and over again. My question is how to write script for this. I imagine this might be a sed or awk script, but I'm not sure. I'm going to start working on a sed as soon as I'm done posting this, but any suggestions?

  • 写回答

2条回答 默认 最新

  • duanbipu7601 2013-12-09 13:18
    关注

    I don't know anything about php, so I'm not sure whether this solution is a particularly good one:

    sed  "s|\[[ ]*|\[\'|g;s|[ ]*\]|\'\]|g" test.in
    

    example of use:

    [fixarraysubscript $] cat test.in
    $array[some_element]
    
    $name1[index]
    
    $name2[index]
    
    
    $name3[ id ]
    
    $name4[ id2 ]
    
    [fixarraysubscript $]
    [fixarraysubscript $] sed  "s|\[[ ]*|\[\'|g;s|[ ]*\]|\'\]|g" test.in
    $array['some_element']
    
    $name1['index']
    
    $name2['index']
    
    
    $name3['id']
    
    $name4['id2']
    
    [fixarraysubscript $]
    

    obviously my input file is somewhat contrived. If this doesn't work for you, please feel free to post some real input.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题