将字符串转换为 php 参数

发布于 2024-12-11 10:12:30 字数 537 浏览 0 评论 0原文

所以假设我有一个函数:

function j($a, $b){
 return $a + $b;
}

然后我将函数的预期参数放入字符串中:

$str = '1,3';

有没有一种方法可以使函数将单个字符串参数视为程序员插入到函数中的参数......因此,当您执行

j($str) 时,

函数不会将 $str 视为单个字符串参数,而是获取字符串的内容并将其视为程序员编写的 j(1,3) 而不是j($str)

这也是一个相当简单的示例,但我希望它适用于更复杂的参数字符串,包括数组、多维数组、关联数组、数组中的数组,以及其中包含逗号的字符串参数(因此仅用逗号进行爆炸并不是真正可行)

我也不想在解决方案中使用 eval()

编辑

另外我试图让它在任何函数上工作,而不仅仅是我的函数(并且不仅仅是这个具体的函数(只是一个毫无价值的示例函数),因此最好在函数外部完成该操作

so suppose I have a function:

function j($a, $b){
 return $a + $b;
}

and then I put the intended arguments of the function into a string:

$str = '1,3';

is there a way to make the function treat the single string argument as if it were the arguments that the programmer inserted into the function....so when you do

j($str),

instead of having the function treat the $str as a single string argument, it fetches the content of the string and treats it as if the programmer wrote j(1,3) instead of j($str)

also this is a rather simple example, but I'd like it to work for even more complicated argument strings involving arrays, multidimensional arrays, associative arrays, array within arrays, as well as string arguments that have commas in it (so just exploding by comma is not really feasible)

also I'd rather not use eval() in the solution

EDIT

Also I'm trying to get this to work on any function not just mine (and not just this specific function which is just a worthless example function) so preferably the operation is to be done outside of the function

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

南巷近海 2024-12-18 10:12:30
call_user_func_array('j', explode(',', $str));

http://www.php.net/call_user_func_array

我不知道你想如何让它工作“更复杂的参数字符串,包括数组的数组”,因为很难将它们表达为字符串。如果您可以将任何字符串格式化为数组,例如使用 JSON 字符串和 json_decode ,那么 call_user_func_array 就是您的朋友。

call_user_func_array('j', explode(',', $str));

http://www.php.net/call_user_func_array

I have no idea how you want to make this work for "more complex argument strings including arrays of arrays", since it's hard to express those as strings. If you can format whatever string you have into an array though, for example using JSON strings and json_decode, call_user_func_array is your friend.

清浅ˋ旧时光 2024-12-18 10:12:30

这应该适用于单个逗号分隔的字符串:

function j($str){
 list($a, $b) = explode(',', $str);
 return $a + $b;
}

您想要在多维数组中求和什么?所有的值?

如果是:您只需检查参数的类型(例如使用 is_array()),然后迭代它。当它是多维时,递归调用你的函数。

This should work for a single, comma-separated string:

function j($str){
 list($a, $b) = explode(',', $str);
 return $a + $b;
}

What do you want to sum in a multi-dimensional array? All of the values?

If yes: You only have to check the type of your argument (e.g. using is_array()) and then iterate through it. When it is multi-dimensional, call your function recursively.

冬天旳寂寞 2024-12-18 10:12:30

将除第一个参数之外的所有参数设为可选,然后使用 list()explode() 如果第一个参数是字符串(或包含,):

function j($a, $b=null){
  if(strpos($a,',')!==false){
    list($a,$b) = explode(',',$a)
  }
  return $a + $b;
}

这只是一个基本的例子,但原理应该很清楚:

  • 检查其中一个参数是否由多个参数组成,
  • 如果是,则自行解析以获取单个参数

make all parameter but except the first one optional and then use list() and explode() if the first parameter is a string (or contains ,):

function j($a, $b=null){
  if(strpos($a,',')!==false){
    list($a,$b) = explode(',',$a)
  }
  return $a + $b;
}

this is just a basic example, but the principle should be clear:

  • check if one of the arguments is composed of multiple parameters
  • if so, parse it on your own to get the single parameters
_失温 2024-12-18 10:12:30
function j($a, $b = 0){ // make second arg optional
  if (! $b) { // is second arg specified and not zero
    if (strpos($a, ',') !== false){ // has provided first arg a comma
       list($first, $second) = explode(',' $a); // yes then get two values from it
       return $first + $second; // return the result
    }
  }
  else {
     return $a + $b; // two args were passed, return the result
  }

}

现在您的函数将支持两种格式,带有一个参数,例如:

$str = '1,3'
j($str);

以及两个参数:

j(5, 10);
function j($a, $b = 0){ // make second arg optional
  if (! $b) { // is second arg specified and not zero
    if (strpos($a, ',') !== false){ // has provided first arg a comma
       list($first, $second) = explode(',' $a); // yes then get two values from it
       return $first + $second; // return the result
    }
  }
  else {
     return $a + $b; // two args were passed, return the result
  }

}

Now your function will support both formats, with one argument eg:

$str = '1,3'
j($str);

as well as two arguments:

j(5, 10);
陌生 2024-12-18 10:12:30

这有效:)

function f($str, $delimiter = ';')
{
    $strargs = explode($delimiter, $str);
    $args = array();
    foreach($strargs as $item)
        eval("\$args[] = " . $item. ";");
    // $args contains all arguments
    print_r($args);
}

检查一下:

f("6;array(8,9);array(array(1,0,8), 5678)");

This works :)

function f($str, $delimiter = ';')
{
    $strargs = explode($delimiter, $str);
    $args = array();
    foreach($strargs as $item)
        eval("\$args[] = " . $item. ";");
    // $args contains all arguments
    print_r($args);
}

Check it:

f("6;array(8,9);array(array(1,0,8), 5678)");
那片花海 2024-12-18 10:12:30

大多数答案都假设所有内容都用逗号“,”很好地分隔开,但情况并非总是如此。例如,可能有不同的变量类型,甚至是带有逗号的字符串。

$values = "123, 'here, is, a, problem, that, can\'t, be, solved, with, explode() function, mkay?'";

这可以通过 php 7 中的 eval() 和点的“...”参数检索来处理。

function helper_string_args(...$args) {
    return $args;
}

$func = "\$values = \\helper_string_args($values);";
try {
    eval($func);
} catch (Exception $e) {
    var_dump($e);
    exit;
}

Most of the answers assume, that everything is nicely separated with coma ",", but it's not always the case. For example there could be different variable types, even strings with coma's.

$values = "123, 'here, is, a, problem, that, can\'t, be, solved, with, explode() function, mkay?'";

This can be handled with eval() and dot's "..." args retrieval in php 7.

function helper_string_args(...$args) {
    return $args;
}

$func = "\$values = \\helper_string_args($values);";
try {
    eval($func);
} catch (Exception $e) {
    var_dump($e);
    exit;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文