$so = file_get_contents(...) ;
$so = preg_replace("/\*([^*]+)\*/e", constant(\1), $so) ;
Run Code Online (Sandbox Code Playgroud)
尝试用相同名称的常量替换星号之间的文本,但我不断收到意外的T_LNUMBER.我写错了什么?我验证了$ so作为字符串的存在,并且所有常量都存在,因为它们是纯文本.
我可能不会使用这个脚本,但我很好奇我搞砸了什么
尝试 preg_replace_callback("/\*([^*]+)\*/e", create_function('$matches','return constant($matches[1]);'), $so)
我一直怀疑php中有匿名函数,但我从来没有使用它们.这里更好:
preg_replace_callback("/\*([^*]+)\*/e", function($matches){
return constant($matches[1]);
}, $so)
Run Code Online (Sandbox Code Playgroud)
编辑:正如下面的评论中所指出的,一旦使用回调,\ e修饰符是不必要的,因为我们不需要评估返回值中的任何代码.