我遇到了一个代码
<?php
define(TRACES_MODE, 'TEXTAREA');//'TEXTAREA' or 'FIREPHP'
$GLOBALS['traces.pre']=array();
function my_array_diff($arr1, $arr2)
{foreach ($arr1 as $k=>$v)
{if (in_array($v, $arr2, true))
{unset($arr1[$k]);
}
}
return $arr1;
}
function my_var_export($var, $is_str=false)
{$rtn=preg_replace(array('/Array\s+\(/', '/\[(\d+)\] => (.*)\n/', '/\[([^\d].*)\] => (.*)\n/'), array('array (', '\1 => \'\2\''."\n", '\'\1\' => \'\2\''."\n"), substr(print_r($var, true), 0, -1));
$rtn=strtr($rtn, array("=> 'array ('"=>'=> array ('));
$rtn=strtr($rtn, array(")\n\n"=>")\n"));
$rtn=strtr($rtn, array("'\n"=>"',\n", ")\n"=>"),\n"));
$rtn=preg_replace(array('/\n +/e'), array('strtr(\'\0\', array(\' \'=>\' \'))'), $rtn);
$rtn=strtr($rtn, array(" Object',"=>" Object'<-"));
if ($is_str)
{return $rtn;
}
else
{echo $rtn;
}
}
function tick_handler()
{$tmp=debug_backtrace();
$trace=my_array_diff($tmp, $GLOBALS['traces.pre']);
//echo '<pre>';var_export($trace);echo '</pre>';echo '<br/>'; //for debug diyism_trace.php
$trace=array_values($trace);
$GLOBALS['traces.pre']=$tmp;
if (count($trace)>0 && $trace[0]['file'].'/'.@$tmp[1]['function']!==@$GLOBALS['traces'][count($GLOBALS['traces'])-1]['key']) //filter empty array and rearrange array_values(), because some lines will trigger two tick events per line, for example: 1.last line is "some code;questmark>" 2.error_reporting(...
{for ($i=count($trace)-1; $i>=0; --$i)
{$GLOBALS['traces'][]=$tmp_fb=array_merge(array('key'=>$trace[$i]['file'].'/'.@$tmp[$i+1]['function']), $trace[$i], array('function'=>strtr($trace[$i]['function'], array('tick_handler'=>'CONTINUE')), 'in_function'=>@$tmp[$i+1]['function']));
TRACES_MODE==='FIREPHP'?fb(trace_output($tmp_fb), 'diyism_trace:'.++$GLOBALS['diyism_trace_no']):'';
}
}
}
function trace_output($trace)
{$trace['in_function']=strtr(@$trace['in_function'], array('require'=>'', 'require_once'=>'', 'include'=>'', 'include_once'=>''));
$trace['args']=$trace['args']?strtr(preg_replace(array('/\n +/'), array(''), preg_replace(array('/\n \d+ => /'), array(''), substr(my_var_export($trace['args'], true), 7, -3))), array("\r"=>'\r', "\n"=>'\n')):'';
return $trace['file'].($trace['in_function']?'/'.$trace['in_function'].'()':'').'/'.$trace['line'].': '.$trace['function'].'('.$trace['args'].')';
}
function traces_output()
{echo '<textarea style="width:100%;height:300px;">';
$GLOBALS['traces']=array_slice($GLOBALS['traces'], 2);//remove registering tick line and requiring 'diyism_trace.php' line
foreach ($GLOBALS['traces'] as $k=>$trace)
{echo htmlentities($k.':'.trace_output($trace)."\n");
}
echo '</textarea>';
}
register_tick_function('tick_handler');
TRACES_MODE==='TEXTAREA'?register_shutdown_function('traces_output'):'';
?>
在上面一行
$rtn=preg_replace(array('/Array\s+\(/', '/\[(\d+)\] => (.*)\n/', '/\[([^\d].*)\] => (.*)\n/'), array('array (', '\1 => \'\2\''."\n", '\'\1\' => \'\2\''."\n"), substr(print_r($var, true), 0, -1));
是我无法理解的。可以帮助我理解这一行中发生的事情。
答案 0 :(得分:0)
它采用人类可读的print_r()
输出(这不是可用的代码)并将其替换为可用的PHP代码以用于数组定义。我本来会使用var_export()
,但这里是模式和替换的3个数组项的简单示例:
//lowercase / remove whitespace
Replace: Array (
With: array (
//get rid of brackets around key / quote the value
Replace: [1] => whatever
With: 1 => 'whatever'
//get rid of brackets around key / quote key / quote value
Replace: [something] => whatever
With: 'something' => 'whatever'
答案 1 :(得分:0)
根据PHP手册,preg_replace可以将数组作为模式和替换的参数。
每个模式分别与替换字符串相关联。并且每个模式都会对字符串进行一次解析。
用你的例子:
patterns replacement strings
/Array\s+\(/ => 'array (' /\[(\d+)\] => (.*)\n/ => '\1 => \'\2\''."\n" /\[([^\d].*)\] => (.*)\n/ => '\'\1\' => \'\2\''."\n"
请注意,使用此功能可以简化这些多次搜索/替换(因为.*
将在换行符处停止):
/Array\s+\(/ => 'array (' /\[(\d+)] => (.*)/ => '\1 => \'\2\'' /\[([^\d][^]]*)] => (.*)/ => '\'\1\' => \'\2\''