REG-实施例以部分地提取存储在一个文件中的阵列 [英] Reg-Ex to partially extract an array stored in a file

查看:155
本文介绍了REG-实施例以部分地提取存储在一个文件中的阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有正确的结构存储在一个文件中像这样的数组:

array.php

 < PHP返回阵列(  0 =>中一个,
  的x=> $时间戳
  Y=>阵列(嵌套=>阵列(1,2,3)),
  Z=>功能()使用(安培; $ VAR){返回$ VAR; },);?>

我应该用什么样的正则表达式来获得一个这样的数组:

  $阵列=的file_get_contents(array.php); //要处理准备一个字符串


  

/ *某种神奇的正则表达式的* /


  $数组[0] =>'A;
$阵列[X] =>'$时间戳';
$阵列[Y] =>'阵列(嵌套=>阵列(1,2,3));
$阵列[Z] ='()的函数使用(安培; $ VAR){返回$ VAR; };


更新::我需要$数组包含这样的价值观,这样我就可以调用eval()函数选择的地方,当我需要它,像这样

  $时间戳=2010-01-01 00:00:00
$ dinamicValue =的eval(回归$阵列[X]。;); //现在是2010-01-01 00:00:00$时间戳=2012-01-01 00:00:00
$ dinamicValue =的eval(回归$阵列[X]。;); //现在是2012-01-01 00:00:00开始

这将用于某种模板系统,这里是一个模板演示 codepad.org / xt56tXnf 其中类似$ DINAMIC_VALUE_ *所有瓦尔应dinamically定义。


解决方案

您可以通过执行acomplish同样的事情

  $时间戳=2013年4月24日12时44分04秒;
$用户ID = 4;$阵列=包括('array.php');

要得到一个部分,你可以实现array.php包含多个条件回报;

If I have an array properly structured stored in a file like this:

array.php

<?php  

return array(

  0=>"a",
  "x"=>$timestamp,
  "y"=>array("nested"=>array(1,2,3)),  
  "z"=>function() use (&$VAR) { return $VAR; },

);  

?>

what kind of RegEx should I use to get an array like this:

$array=file_get_contents("array.php"); // a string ready to be processed

/* some kind of magic regex */

$array[0]=>'"a"';
$array["x"]=>'$timestamp';
$array["y"]=>'array("nested"=>array(1,2,3))';  
$array["z"]='function() use (&$VAR) { return $VAR; }';

?


update :: i need $array contains values like these, so i can call eval() selectively where and when i need it, like this

$timestamp="2010-01-01 00:00:00";
$dinamicValue=eval(" return ".$array["x"]."; "); // now is "2010-01-01 00:00:00"

$timestamp="2012-01-01 00:00:00";
$dinamicValue=eval(" return ".$array["x"]."; "); // now is "2012-01-01 00:00:00"

it will be used for some kind of template system, here is a template demo codepad.org/xt56tXnf where all vars like $DINAMIC_VALUE_* should be defined dinamically.

解决方案

You can acomplish the same thing by doing

$timestamp="2013-04-24 12:44:04";  
$userid=4;

$array=include('array.php');

To get a partial you could implement the array.php to contain multiple conditional return;

这篇关于REG-实施例以部分地提取存储在一个文件中的阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆