dongliping003116 2019-05-03 13:44
浏览 45
已采纳

如何使用正则表达式在PHP代码中捕获不带引号的数组索引并引用它们?

PHP 7.2 upgraded undefined constant errors from a notice to a warning, with advice that in future they will return a full-on error instead.

I am trying to identify a way to fix these via scripting, ideally via a regex that I can run to parse each PHP file on a site, find all offending bits of code, and fix them.

I've found multiple examples of how to fix one variant, but none for another, and it's that one that I'm looking for help with.

Here's an example file:

<?php

$array[foo] = "bar"; 
// this should become 
// $array['foo'] = "bar"

echo "hello, my name is $array[foo] and it's nice to meet you"; 
// would need to become 
// echo "hello, my name is " . $array['foo'] . " and it's nice to meet you";

?>

I've seen a lot of options to identify and change the first type, but none for the second, where the undefined constant is within a string. In that instance the parser would need to:

  1. Replace $array[foo] with $array['foo']
  2. Find the entire variable, end quotes beforehand, put a . either side, and then reopen quotes afterwards

Edit: ideally one regexp would deal with both examples in the sample code in one pass - i.e. add the ticks, and also add the quotes/dots if it identifies it’s within a string.

  • 写回答

2条回答 默认 最新

  • douren7921 2019-05-05 19:51
    关注
    $array[foo] = "bar"; 
    // this should become 
    // $array['foo'] = "bar"
    

    Yes, this has always triggered a notice and has always been poor practice.

    echo "hello, my name is $array[foo] and it's nice to meet you"; 
    // would need to become 
    // echo "hello, my name is " . $array['foo'] . " and it's nice to meet you";
    

    No, this style has never triggered a notice and does not now. In fact, it's used as an example in the PHP documentation. PHP is never going to remove the ability to interpolate array variables in strings.


    Your first case is easy enough to catch with something like this:

    $str = '$array[foo] = "bar";';
    echo preg_replace("/(\\$[a-z_][a-z0-9_]*)\\[([a-z][a-z0-9_]*)\\]/", "$1['$2']", $str);
    

    But of course needs to be caught only outside of a string.

    As with any complex grammar, regular expressions will never be as reliable as a grammar-specific parser. Since you're parsing PHP code, your most accurate solution will be to use PHP's own token parser.

    $php = <<< 'PHP'
    <?php
    $array[foo] = "bar"; // this line should be the only one altered.
    $array['bar'] = "baz";
    echo "I'm using \"$array[foo]\" and \"$array[bar]\" in a sentence";
    echo 'Now I\'m not using "$array[foo]" and "$array[bar]" in a sentence';
    PHP;
    
    $tokens = token_get_all($php);
    $in_dq_string = false;
    $last_token = null;
    $output = "";
    
    foreach ($tokens as $token) {
        if ($last_token === "[" && is_array($token) && $token[0] === 319 && !$in_dq_string) {
            $output .= "'$token[1]'";
        } elseif (is_array($token)) {
            $output .= $token[1];
        } else {
            if ($token === "\"") {
                $in_dq_string = !$in_dq_string;
            }
            $output .= $token;
        }
        $last_token = $token;
    }
    
    echo $output;
    

    Output:

    <?php
    $array['foo'] = "bar"; // this line should be the only one altered.
    $array['bar'] = "baz";
    echo "I'm using \"$array[foo]\" and \"$array[bar]\" in a sentence";
    echo 'Now I\'m not using "$array[foo]" and "$array[bar]" in a sentence';
    

    This code would need some edge cases accounted for, such as when you are intentionally using a constant as an array index.

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

报告相同问题?

悬赏问题

  • ¥15 基于单片机AT89C51下的交通信号灯设计
  • ¥15 数电设计题 没有设计思路 不知道用什么芯片进行设计 求提供设计思路
  • ¥15 在动态多目标优化问题中,第一幅图展示的是问题DF6的相关定义和绘制的POS和POF图,请问图中公式PS(t)和PF(t)是如何推导的
  • ¥60 设计一种优化算法结合案例给出智能仓储四向穿梭车的调度计划
  • ¥15 Errno2:No such file or directory,在当前文件确实没有该图片,怎么解决?
  • ¥15 博世摄像头数据存储的问题(iscsi)
  • ¥15 如何实现对学生籍贯信息管理系统的选择排序
  • ¥15 写一个51单片机的时钟代码
  • ¥15 git clone报错
  • ¥15 3d-slicer超声造影动态图像导入报错