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

前端之家收集整理的这篇文章主要介绍了正则表达式部分提取PHP代码((数组定义))前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个这样的字符串存储((数组定义))的 PHP代码
  1. $code=' array(
  2.  
  3. 0 => "a","a" => $GlobalScopeVar,"b" => array("nested"=>array(1,2,3)),"c" => function() use (&$VAR) { return isset($VAR) ? "defined" : "undefined" ; },); ';

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

  1. $array=(
  2.  
  3. 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代码时,什么代码更容易理解.

  1. $code = ' array(
  2.  
  3. 0 => "a","string_literal",12345
  4.  
  5. ); ';
  6.  
  7. $token = token_get_all("<?PHP ".$code);
  8. $newcode = "";
  9.  
  10. $i = 0;
  11. while (++$i < count($token)) { // enter into array; then start.
  12. if (is_array($token[$i]))
  13. $newcode .= $token[$i][1];
  14. else
  15. $newcode .= $token[$i];
  16.  
  17. if ($token[$i] == "(") {
  18. $ending = ")";
  19. break;
  20. }
  21. if ($token[$i] == "[") {
  22. $ending = "]";
  23. break;
  24. }
  25. }
  26.  
  27. // init variables
  28. $escape = 0;
  29. $wait_for_non_whitespace = 0;
  30. $parenthesis_count = 0;
  31. $entry = "";
  32.  
  33. // main loop
  34. while (++$i < count($token)) {
  35. // don't match commas in func($a,$b)
  36. if ($token[$i] == "(" || $token[$i] == "{") // ( -> normal parenthesis; { -> closures
  37. $parenthesis_count++;
  38. if ($token[$i] == ")" || $token[$i] == "}")
  39. $parenthesis_count--;
  40.  
  41. // begin new string after T_DOUBLE_ARROW
  42. if (!$escape && $wait_for_non_whitespace && (!is_array($token[$i]) || $token[$i][0] != T_WHITESPACE)) {
  43. $escape = 1;
  44. $wait_for_non_whitespace = 0;
  45. $entry .= "'";
  46. }
  47.  
  48. // here is a T_DOUBLE_ARROW,there will be a string after this
  49. if (is_array($token[$i]) && $token[$i][0] == T_DOUBLE_ARROW && !$escape) {
  50. $wait_for_non_whitespace = 1;
  51. }
  52.  
  53. // entry ended: comma reached
  54. if (!$parenthesis_count && $token[$i] == "," || ($parenthesis_count == -1 && $token[$i] == ")" && $ending == ")") || ($ending == "]" && $token[$i] == "]")) {
  55. // go back to the first non-whitespace
  56. $whitespaces = "";
  57. if ($parenthesis_count == -1 || ($ending == "]" && $token[$i] == "]")) {
  58. $cut_at = strlen($entry);
  59. while ($cut_at && ord($entry[--$cut_at]) <= 0x20); // 0x20 == " "
  60. $whitespaces = substr($entry,$cut_at + 1,strlen($entry));
  61. $entry = substr($entry,$cut_at + 1);
  62. }
  63.  
  64. // $escape == true means: there was somewhere a T_DOUBLE_ARROW
  65. if ($escape) {
  66. $escape = 0;
  67. $newcode .= $entry."'";
  68. } else {
  69. $newcode .= "'".addcslashes($entry,"'\\")."'";
  70. }
  71.  
  72. $newcode .= $whitespaces.($parenthesis_count?")":(($ending == "]" && $token[$i] == "]")?"]":","));
  73.  
  74. // reset
  75. $entry = "";
  76. } else {
  77. // add actual token to $entry
  78. if (is_array($token[$i])) {
  79. $addChar = $token[$i][1];
  80. } else {
  81. $addChar = $token[$i];
  82. }
  83.  
  84. if ($entry == "" && $token[$i][0] == T_WHITESPACE) {
  85. $newcode .= $addChar;
  86. } else {
  87. $entry .= $escape?str_replace(array("'","\\"),array("\\'","\\\\"),$addChar):$addChar;
  88. }
  89. }
  90. }
  91.  
  92. //append remaining chars like whitespaces or ;
  93. $newcode .= $entry;
  94.  
  95. print $newcode;

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

输出

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

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

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

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