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 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同