将函数参数列表转换为数组

发布于 2024-11-10 16:13:38 字数 654 浏览 0 评论 0原文

我正在开发 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

眼泪淡了忧伤 2024-11-17 16:13:38

我想如果你真的想从字符串开始(而不是像其他人建议的那样从数组开始),你可以这样做:

In PHP 5.3:

$params = "'this is, parameter 1', 'this is parameter2'";
$paramsarray = str_getcsv($params, ',', "'");

In PHP 5.1/5.2:

$fp = fopen('php://temp', 'w+');
fwrite($fp, $params);
fseek($fp, 0);
$paramsarray = fgetcsv($fp, 0, ',', "'");

print_r($paramsarray);

...and get:

Array
(
    [0] => this is, parameter 1
    [1] => this is parameter2
)

...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:

$params = "'this is, parameter 1', 'this is parameter2'";
$paramsarray = str_getcsv($params, ',', "'");

In PHP 5.1/5.2:

$fp = fopen('php://temp', 'w+');
fwrite($fp, $params);
fseek($fp, 0);
$paramsarray = fgetcsv($fp, 0, ',', "'");

print_r($paramsarray);

...and get:

Array
(
    [0] => this is, parameter 1
    [1] => this is parameter2
)

...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.

峩卟喜欢 2024-11-17 16:13:38

也许您可以使用 func_get_args 来实现此目的?

另外,我认为 call_user_func 应该这样调用:

    call_user_func('functionName', $param1, $param2);

Maybe you could just use func_get_args for this?

Also, call_user_func I believe should be called like this:

    call_user_func('functionName', $param1, $param2);
遮了一弯 2024-11-17 16:13:38

$params 是(在您的情况下)单个变量,其中包含 string 类型的单个值。它不是数组或任何其他复杂类型。我认为您甚至不需要 %Complex Regex%

$funcname = 'testfunction';
$params = "'this is, parameter 1', 'this is parameter2'";

foreach ($params as &$param) $param = trim($param, "' \n\r\t");

echo call_user_func_array($funcname, $params);

$params is (in your case) a single variable, that contains a single value of type string. Its not an array or any other complex type. I assume, that you don't even need your %Complex Regex%.

$funcname = 'testfunction';
$params = "'this is, parameter 1', 'this is parameter2'";

foreach ($params as &$param) $param = trim($param, "' \n\r\t");

echo call_user_func_array($funcname, $params);
拥抱没勇气 2024-11-17 16:13:38

听起来你想要 call_user_func_array

$params = array('this is, parameter 1', 'this is parameter2');
$funcname = 'testfunction';

echo call_user_func_array($funcname, $params);

Sounds like you want call_user_func_array instead.

$params = array('this is, parameter 1', 'this is parameter2');
$funcname = 'testfunction';

echo call_user_func_array($funcname, $params);
↘人皮目录ツ 2024-11-17 16:13:38

尝试使用 call_user_func_array 代替。

Try maybe using call_user_func_array instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文