将函数参数列表转换为数组
我正在开发 CMS,并且正在寻找一种将函数参数列表转换为数组的方法。例如:
function testfunction($param1, $param2){
$string = "Param1: $param1 Param2: $param2";
return $string;
}
$funcname = 'testfunction';
$params = "'this is, parameter 1', 'this is parameter2'";
//This doesnt work, sends both parameters as the first, dont know why.
echo call_user_func($funcname, $params);
//So I want to split the parameter list:
$paramsarray = preg_split('%Complex Regex%', $params);
//And call thusly:
echo call_user_func_array($funcname, $paramsarray);
我不知道这里使用什么样的正则表达式...... 我可以用“,”来爆炸,但这会爆炸字符串、数组等中包含的所有逗号...所以我需要一个正则表达式来做到这一点,我对正则表达式没问题,但似乎会有很多规则在这。
I'm working on a CMS, and I'm looking for a way to convert a list of function arguments, into an array. For example:
function testfunction($param1, $param2){
$string = "Param1: $param1 Param2: $param2";
return $string;
}
$funcname = 'testfunction';
$params = "'this is, parameter 1', 'this is parameter2'";
//This doesnt work, sends both parameters as the first, dont know why.
echo call_user_func($funcname, $params);
//So I want to split the parameter list:
$paramsarray = preg_split('%Complex Regex%', $params);
//And call thusly:
echo call_user_func_array($funcname, $paramsarray);
I dont know what kind of regex to use here....
I could just explode by ',' but that would explode all commas contained in strings, arrays etc... So I need a regex to do this, I'm ok with regexes, but it seems like there would be a lot of rules in this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我想如果你真的想从字符串开始(而不是像其他人建议的那样从数组开始),你可以这样做:
In PHP 5.3:
In PHP 5.1/5.2:
...and get:
...then use
call_user_func_array
。如果您想使用更复杂的类型(例如:数组或对象),那将是一个真正的挑战。您可能必须使用分词器。
I guess if you really want to start from a string (instead of an array like others suggested), you could do:
In PHP 5.3:
In PHP 5.1/5.2:
...and get:
...then use
call_user_func_array
.If you want to use more complex types (e.g.: arrays or objects), that'll be a real challenge. You'll probably have to use the Tokenizer.
也许您可以使用 func_get_args 来实现此目的?
另外,我认为 call_user_func 应该这样调用:
Maybe you could just use func_get_args for this?
Also, call_user_func I believe should be called like this:
$params
是(在您的情况下)单个变量,其中包含string
类型的单个值。它不是数组或任何其他复杂类型。我认为您甚至不需要%Complex Regex%
。$params
is (in your case) a single variable, that contains a single value of typestring
. Its not an array or any other complex type. I assume, that you don't even need your%Complex Regex%
.听起来你想要 call_user_func_array 。
Sounds like you want call_user_func_array instead.
尝试使用
call_user_func_array
代替。Try maybe using
call_user_func_array
instead.