dongxian1699 2015-01-07 10:21
浏览 98
已采纳

使用没有引号的数组键,如何在大项目上修复它?

I started a project using much arrays keys without quotes.

And now I having problems with this method, i didn't knew it was bad when i started my project. I finally wanted to display E_NOTICES errors for reasons but it crash because the log is overloaded with millions notices like PHP Notice: Use of undefined constant message - assumed 'key'.

So to fix it I could add quotes to my keys in my whole project but there are so much ! Is there a way to achieve this with an algorithme or anything to fix my code ? I want replace any undefined constant by a string with quotes, EG: $my_array[key] by $my_array['key'].

Thanks.

EDIT: I succeeded to fix all declarations using rejex, like this:

\[([^0-9\$\'\"\]])([^\'\"\]]*)\] to \[\'\1\2\'\]

But it is not enough, there are much situations where unquoted keys are used without brackets, EG:

array_key_exists(unquotedKey,$array)

$array['key'] = array( unquotedKey => array(96,56) );

etc...

I could fix all situations using regex but I guess I will have much troubles to handle it well, and sometimes keys of my arrays are really constants and it shouldn't be quoted ! If anybody have a better solution it would help me a lot.

The perfect solution would be to be able to get my code after PHP replaced undefined constants by quoted strings, is it possible ? It does it each time I compile, it is maybe stored somewhere temporarily.

  • 写回答

1条回答 默认 最新

  • drurhg37071 2015-01-07 10:34
    关注

    I use Notepad++ which has a search and replace in files feature (Ctrl + Shift + F). With regular expression mode on, you could use

    Search:

    \$my_array\[([^\'\"]+)\]
    

    Replace

    \$my_array\[\'$1\'\]
    

    The search looks for anything within the array key square brackets where there is not already a " or ' character, which would indicate the declaration is already valid.

    Select the directory of your project then hit "Replace in Files". Ensure your entire project is backed up first in case something goes wrong.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看