一个PHP正则表达式从代码文件中提取php函数 [英] A PHP regex to extract php functions from code files
本文介绍了一个PHP正则表达式从代码文件中提取php函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图让PHP正则表达式从php源代码中提取函数。直到现在,我使用递归正则表达式来提取{}之间的所有内容,但是它也匹配if语句之类的内容。当我使用类似的东西时:
preg_match_all(/(function。* \(。* \))({( [^ {}] + |(?R))*})/,$ data,$ matches);
当文件中有超过1个函数时它不工作(可能是因为它使用'$'
$ b
有没有办法做到这一点?
示例文件:
<?php
if($ useless)
{
echoi不想要这个;
}
函数bla($ wut)
{
echo我确实需要这个;
}
?>
谢谢
解决方案正则表达式是做错的方法。考虑标记器或反思
I'm trying to make a PHP regex to extract functions from php source code. Until now i used a recursive regex to extract everything between {} but then it also matches stuff like if statements. When i use something like:
preg_match_all("/(function .*\(.*\))({([^{}]+|(?R))*})/", $data, $matches);
It doesn't work when there is more than 1 function in the file (probably because it uses the 'function' part in the recursiveness too).
Is there any way to do this?
Example file:
<?php
if($useless)
{
echo "i don't want this";
}
function bla($wut)
{
echo "i do want this";
}
?>
Thanks
解决方案
regexps is the wrong way to do it. Consider tokenizer or reflection
这篇关于一个PHP正则表达式从代码文件中提取php函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文