简体   繁体   English

使用正则表达式从PHP中解析字符串中的函数

[英]Using Regular Expressions to parse functions from a string in PHP

Is there a logical way to strip out function names and their arguments using Regular Expressions in PHP? 有没有一种逻辑方法可以在PHP中使用正则表达式去掉函数名称及其参数? Currently I have it splitting line by line so that I am able to have each function on each line for easier markup. 目前我将它逐行拆分,这样我就可以在每一行上使用每个函数以便于标记。

So: 所以:

doSomeFunction(arg, moreargs, stuff);
breakSomething();
ini_set(include_path, /home/htdocs/);

becomes

array([0] => 'doSomeFunction(arg, moreargs, stuff);', [1] => 'breakSomething();' and so on...

The real problem I am having is getting the function name using regex to have syntax like: 我遇到的真正问题是使用正则表达式获取函数名称具有如下语法:

$functionfromregex($args);

The functions are constructed using PHP's "Variable Function Name" ability, so it is possible. 这些函数是使用PHP的“变量函数名”功能构建的,因此它是可行的。 Arguments are taken in as arrays since there is no reasonable way to glue them togather the way that PHP needs to parse them as. 参数被作为数组使用,因为没有合理的方法来粘合它们以收集PHP需要解析它们的方式。 The real downside is that I am not too familiar with Regular Expressions, any help will be greatly appreciated, and if you need any other information feel free to ask. 真正的缺点是我对正则表达式不太熟悉,任何帮助将不胜感激,如果您需要任何其他信息,请随时询问。

如果要解析PHP,可以使用token_get_all获取给定代码的标记

您还可以查看Reflection类。

So I'm not sure in php, I'm doing something similar in java and the following code strips it in a similar way: 所以我不确定在PHP中,我在java中做类似的事情,以下代码以类似的方式剥离它:

[^(&&[^)&&[^,]]] applied to foo(param1, param2, param3) will return ["foo", "param1", "param2", "param3"]. [^(&& [^)&& [^,]]]应用于foo(param1,param2,param3)将返回[“foo”,“param1”,“param2”,“param3”]。

The problem I'm having is that this will string out all '(' and ')', so there is no way to tell if your param is actually a variable or a method. 我遇到的问题是这会将所有'('和')'串起来,所以没有办法判断你的param实际上是变量还是方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM