我有一个带有正则表达式的脚本,它检测php中所有已定义的内部函数。该脚本运行良好,但是换行符存在问题。
使用此脚本,我从PHP搜索内部和定义的功能。每个函数都必须用新函数“no_function()”替换。
$functions=get_defined_functions();
for($i=0;$i<count($functions["internal"]);$i++){
//Include array functions
if($functions["internal"][$i]=="array"||$functions["internal"][$i]=="array_push"){
}else{
$code=preg_replace('#('.$functions["internal"][$i].'[ |\n|\r|\t]*\([ |\n|\r|\t]*.*\))#i',"no_function()",$code);
}
}
当我有这样的功能时:
str_replace($search,$replace,$string);
该函数不会被调用,因为它将替换为 no_function();
它会正常工作。但如果我有这样的功能:
str_replace($search,
$replace,
$string);
正则表达式没有检测到,但PHP也会执行它。我是正则表达式的菜鸟。有人能帮助我吗?
感谢您的每一个回复。
答案 0 :(得分:0)
不确定您的用例。但是你可以使用命名空间并重新声明你的命名空间下的内部函数,以便快速破解内部函数而不会返回。
首先制作一个由虚拟内部函数组成的文件。
<?php
$funcs = get_defined_functions();
$output = "<?php\n";
$output .= "namespace example;";
foreach ($funcs['internal'] as $func_name) {
$output.= "function $func_name () {}\n";
}
file_put_contents('dummy_funcs.php', $output);
然后在另一个文件(可能是一个常见的引导程序)中尽早包含你的虚函数。
<?php
namespace example;
include 'dummy_funcs.php';
echo str_replace('w', 'l', 'bawls');
什么都不输出。
这里的结果是您不需要编辑代码(除了添加include之外)。但是,根据您现有的命名空间,它可能会很痛苦。