应该输出:数组(0 =>'一种"',一个"=>'$GlobalScopeVar',b"=>'数组(嵌套"=>数组(1,2,3))',c"=>'function() use (&$VAR) { return isset($VAR) ?已定义":未定义";}','字符串字面量"','12345')你可以,获取数组的数据,print_r(eval("return $newcode;"));获取数组的条目:数组([0] =>一种"[a] =>$GlobalScopeVar[b] =>数组(嵌套"=> 数组(1,2,3))[c] =>function() use (&$VAR) { return isset($VAR) ?已定义":未定义";}[1] =>字符串字面量"[2] =>12345)I have php code stored (( array definition )) in a string like this$code=' array( 0 => "a", "a" => $GlobalScopeVar, "b" => array("nested"=>array(1,2,3)), "c" => function() use (&$VAR) { return isset($VAR) ? "defined" : "undefined" ; },); ';there is a regular expression to extract this array??, i mean i want something like$array=( 0 => '"a"', 'a' => '$GlobalScopeVar', 'b' => 'array("nested"=>array(1,2,3))', 'c' => 'function() use (&$VAR) { return isset($VAR) ? "defined" : "undefined" ; }',);pD :: i do research trying to find a regular expression but nothing was found.pD2 :: gods of stackoverflow, let me bounty this now and i will offer 400 :3pD3 :: this will be used in a internal app, where i need extract an array of some php file to be 'processed' in parts, i try explain with this codepad.org/td6LVVme 解决方案 Even when you asked for a regex, it works also with pure PHP. token_get_all is here the key function. For a regex check @HamZa's answer out.The advantage here is that it is more dynamic than a regex. A regex has a static pattern, while with token_get_all, you can decide after every single token what to do. It even escapes single quotes and backslashes where necessary, what a regex wouldn't do.Also, in regex, you have, even when commented, problems to imagine what it should do; what code does is much easier to understand when you look at PHP code.$code = ' array( 0 => "a", "a" => $GlobalScopeVar, "b" => array("nested"=>array(1,2,3)), "c" => function() use (&$VAR) { return isset($VAR) ? "defined" : "undefined" ; }, "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 loopwhile (++$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, 0, $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;Should output:array( 0 => '"a"', "a" => '$GlobalScopeVar', "b" => 'array("nested"=>array(1,2,3))', "c" => 'function() use (&$VAR) { return isset($VAR) ? "defined" : "undefined" ; }', '"string_literal"', '12345') You can, to get the array's data, print_r(eval("return $newcode;")); to get the entries of the array:Array( [0] => "a" [a] => $GlobalScopeVar [b] => array("nested"=>array(1,2,3)) [c] => function() use (&$VAR) { return isset($VAR) ? "defined" : "undefined" ; } [1] => "string_literal" [2] => 12345) 这篇关于部分提取php代码的正则表达式((数组定义))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-31 18:14