如何从包含函数的字符串中获取函数体?
这是字符串:
<?php
$string = '//ps: this placeholder text to demonstrate issues..
function go($url) {
header("Location: {$url}");
exit();
}
function some() {
/* this is the function body */
$result = mysql_query("SELECT something FROM sometable");
while ($row = mysql_fetch_assoc($result)) {
//something...
}
return "something";
/* this is the function body */
}
function fetch2($result) {
while ($row = mysql_fetch_assoc($result)) {
//something...
}
return "something";
}';
?>
我尝试过正则表达式,它在某些情况下有效,而在其他情况下则不然(如果函数体内有大括号,我无法控制贪婪)。
这就是我尝试过的:
preg_match('#function some\(\)\s*{(.+?)}#s', $string, $matches);
echo $match[1];
但是,由于右大括号之前的大括号集干扰了匹配,因此这并没有给我完整的函数体。
我的问题(详细说明):我怎样才能更可靠地获得函数体(当它们的大括号在右大括号之前时不会很麻烦)?有人建议使用 token_get_all()
但我不知道如何使用它或者我应该用它做什么。
Heres the string:
<?php
$string = '//ps: this placeholder text to demonstrate issues..
function go($url) {
header("Location: {$url}");
exit();
}
function some() {
/* this is the function body */
$result = mysql_query("SELECT something FROM sometable");
while ($row = mysql_fetch_assoc($result)) {
//something...
}
return "something";
/* this is the function body */
}
function fetch2($result) {
while ($row = mysql_fetch_assoc($result)) {
//something...
}
return "something";
}';
?>
I've tried regex it works on some occasions and other occasions it doesn't (if there are braces within the function body I can't control the greedyness).
This is what I tried:
preg_match('#function some\(\)\s*{(.+?)}#s', $string, $matches);
echo $match[1];
However, this does not give me the complete function body due to the set of braces before the closing brace interfering with the matching.
My question (ellaborated): How can I get the function body more reliably (without it being troublesome when theirs braces before the closing brace)? Someone suggested to use token_get_all()
but I have no clue how to use it or what I should use it for.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以尝试这样的事情
You can try something like this
这是另一种直接从文件读取的替代解决方案:
Here is another alternative solution that reads directly from a file:
今天正好需要一个工具来检查源码
PHP 函数是即时运行的,并没有真正看到任何有趣的东西,
我将下面粘贴的使用一些 PHP 反射类的类放在一起。
它不仅会即时为您提供 PHP 函数的源代码,
但它还会为您提供有关函数参数的详细信息(如果可用)
因此,如果您正在询问的函数是这样的......
...该类除了函数的源代码之外,还会为您提供详细信息
像这样的字段信息...
PHP 的反射类没有很好的文档记录,(通常没有文档),
但今天有一点烦躁,同样允许创作
下面的 PHP“func_def”类。
我构建它是为了支持更大的工作,让我可以通过类型检查等从 Javascript 安全地实时调用 PHP 函数。
享受!
例子:
Just happened today to have a need for a tool to examine the source code of
PHP functions on-the-fly, and not really seeing anything interesting around ,
I tossed together the class pasted down below that uses some of PHP's reflection classes.
It will not only give you the source code of the PHP function , on-the-fly,
but it will also give you detailed information on the function's parameters (when available)
So if the function you were interrogating was something like this....
... The class would aside from the function's source , give you detailed
field information like this...
PHP's reflection classes are not well documented ,(often no documentation),
but there was enough that with a little fidgeting today, same allowed the creation
of the PHP "func_def" class below.
I built it to support a larger effort that lets me securely call PHP functions in real time from Javascript with type checking and such.
Enjoy!
EXAMPLE: