正则表达式部分提取PHP代码((数组定义))

前端之家收集整理的这篇文章主要介绍了正则表达式部分提取PHP代码((数组定义))前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个这样的字符串存储((数组定义))的 PHP代码
$code=' array(

  0  => "a","a" => $GlobalScopeVar,"b" => array("nested"=>array(1,2,3)),"c" => function() use (&$VAR) { return isset($VAR) ? "defined" : "undefined" ; },); ';

有一个正则表达式来提取这个数组??,我的意思是我想要的东西

$array=(  

  0  => '"a"','a' => '$GlobalScopeVar','b' => 'array("nested"=>array(1,3))','c' => 'function() use (&$VAR) { return isset($VAR) ? "defined" : "undefined" ; }',);

pD ::我做研究试图找到一个正则表达式,但没有发现.
pD2 :: godover of stackoverflow,让我现在赏金,我会提供400:3
pD3 ::这将在一个内部应用程序中使用,我需要提取一些PHP文件的数组被处理的部分,我试着用这个codepad.org/td6LVVme解释

即使您要求正则表达式,也可以使用纯PHP. token_get_all这里是关键功能.对于正则表达式检查 @HamZa’s answer出来.

这里的优点是它比正则表达式更有活力.正则表达式具有静态模式,而使用token_get_all,您可以在每个单个令牌之后决定要做什么.它甚至可以在必要时转义单引号和反斜杠,正则表达式不会做什么.

另外,在正则表达式中,即使有评论,你也有想象它应该做什么的问题;当您查看PHP代码时,什么代码更容易理解.

$code = ' array(

  0  => "a","string_literal",12345

); ';

$token = token_get_all("<?PHP ".$code);
$newcode = "";

$i = 0;
while (++$i < count($token)) { // enter into array; then start.
        if (is_array($token[$i]))
                $newcode .= $token[$i][1];
        else
                $newcode .= $token[$i];

        if ($token[$i] == "(") {
                $ending = ")";
                break;
        }
        if ($token[$i] == "[") {
                $ending = "]";
                break;
        }
}

// init variables
$escape = 0;
$wait_for_non_whitespace = 0;
$parenthesis_count = 0;
$entry = "";

// main loop
while (++$i < count($token)) {
        // don't match commas in func($a,$b)
        if ($token[$i] == "(" || $token[$i] == "{") // ( -> normal parenthesis; { -> closures
                $parenthesis_count++;
        if ($token[$i] == ")" || $token[$i] == "}")
                $parenthesis_count--;

        // begin new string after T_DOUBLE_ARROW
        if (!$escape && $wait_for_non_whitespace && (!is_array($token[$i]) || $token[$i][0] != T_WHITESPACE)) {
                $escape = 1;
                $wait_for_non_whitespace = 0;
                $entry .= "'";
        }

        // here is a T_DOUBLE_ARROW,there will be a string after this
        if (is_array($token[$i]) && $token[$i][0] == T_DOUBLE_ARROW && !$escape) {
                $wait_for_non_whitespace = 1;
        }

        // entry ended: comma reached
        if (!$parenthesis_count && $token[$i] == "," || ($parenthesis_count == -1 && $token[$i] == ")" && $ending == ")") || ($ending == "]" && $token[$i] == "]")) {
                // go back to the first non-whitespace
                $whitespaces = "";
                if ($parenthesis_count == -1 || ($ending == "]" && $token[$i] == "]")) {
                        $cut_at = strlen($entry);
                        while ($cut_at && ord($entry[--$cut_at]) <= 0x20); // 0x20 == " "
                        $whitespaces = substr($entry,$cut_at + 1,strlen($entry));
                        $entry = substr($entry,$cut_at + 1);
                }

                // $escape == true means: there was somewhere a T_DOUBLE_ARROW
                if ($escape) {
                        $escape = 0;
                        $newcode .= $entry."'";
                } else {
                        $newcode .= "'".addcslashes($entry,"'\\")."'";
                }

                $newcode .= $whitespaces.($parenthesis_count?")":(($ending == "]" && $token[$i] == "]")?"]":","));

                // reset
                $entry = "";
        } else {
                // add actual token to $entry
                if (is_array($token[$i])) {
                        $addChar = $token[$i][1];
                } else {
                        $addChar = $token[$i];
                }

                if ($entry == "" && $token[$i][0] == T_WHITESPACE) {
                        $newcode .= $addChar;
                } else {
                        $entry .= $escape?str_replace(array("'","\\"),array("\\'","\\\\"),$addChar):$addChar;
                }
        }
}

//append remaining chars like whitespaces or ;
$newcode .= $entry;

print $newcode;

演示于:http://3v4l.org/qe4Q1

输出

array(

  0  => '"a"',"a" => '$GlobalScopeVar',"b" => 'array("nested"=>array(1,"c" => 'function() use (&$VAR) { return isset($VAR) ? "defined" : "undefined" ; }','"string_literal"','12345'

)

你可以得到数组的数据,print_r(eval(“return $newcode;”));获取数组的条目:

Array
(
    [0] => "a"
    [a] => $GlobalScopeVar
    [b] => array("nested"=>array(1,3))
    [c] => function() use (&$VAR) { return isset($VAR) ? "defined" : "undefined" ; }
    [1] => "string_literal"
    [2] => 12345
)

猜你在找的正则表达式相关文章