如何将单个php文件中定义的所有常量转换为数组?
这是一个定义了常量的php文件。
我想把这个页面中的所有常量都放到一个数组中;
我该如何做这项工作呢?
<?php
/**
* hello wolrd
----------------------------------------
*/
define('HELLO_WORLD','hello world');
define('GOOD_BOOD','
very good
');
define(
'JY_CREDITS_HTML',
'
<p>hello wrold</p>
<div class="good">
<span>world</span>
</div>
'
);
// define('META_KEY','Wat adidi');
define('XXX',"xxx")
/*
hello world
*/
?>结果应该是
<?php
$result=array(
'HELLO_WORLD' => 'hello wrold',
'GOOD_BOOD' => 'very good',
'JY_CREDITS_HTML' =>'....',
'XXX'=>'xxx'
);
?>发布于 2013-05-23 19:51:16
print_r( get_defined_constants(true)['user'] );发布于 2013-05-23 19:52:16
<?php
print_r(get_defined_constants(true));
?>发布于 2013-05-23 19:53:08
你可以使用下面这样的东西:-
define('HELLO_WORLD','hello world');
define('GOOD_BOOD','
very good
');
define(
'JY_CREDITS_HTML',
'
<p>hello wrold</p>
<div class="good">
<span>world</span>
</div>
'
);
define('XXX',"xxx");
$result = get_defined_constants(true);
if(isset($result['user'])){
$result = $result['user']; //just incase there are no user constants set.
}
var_dump($result);输出:-
array (size=4)
'HELLO_WORLD' => string 'hello world' (length=11)
'GOOD_BOOD' => string '
very good
' (length=17)
'JY_CREDITS_HTML' => string '
<p>hello wrold</p>
<div class="good">
<span>world</span>
</div>
' (length=92)
'XXX' => string 'xxx' (length=3)查看此处http://www.php.net/manual/en/function.get-defined-constants.php
https://stackoverflow.com/questions/16713141
复制相似问题