Call php function from string (with parameters)

I want to dynamically run a php function using this line:

do_lightbox('image1.jpg', 'picture 1')

I parsed the line as follows:

$exe = "do_lightbox";
$pars = "'image1.jpg', 'picture 1'";

and tried using the following code:

$rc = call_user_func($exe, $pars);

Unfortunately this gives me an error - I also tried splitting $ pars, for example

$pars = explode(',', $pars);

but it didn’t help ..

any ideas? thank

+5
source share
6 answers

I think this is what you need:

$exe = "do_lightbox";
$pars = array('image1.jpg', 'picture 1');

$rc = call_user_func_array($exe, $pars);
+4
source

$ pars should be an array with parameters in it. It should be: array('image1.jpg', 'picture 1')but with your method this is: array("'image1.jpg'", " 'picture 1'")which is not what you are looking for.

+2
source

, call_user_func():

function myfunc($p1,$p2){
    echo "first: $p1, second: $p2\n";
}

$a1="someval";
$a2="someotherval";
call_user_func("myfunc",$a1,$a2);

, . , :

function myfunc($p1,$p2){
    echo "first: $p1, second: $p2\n";
}

$a="someval, someotherval";
$e=explode(", ",$a);
$a1=$e[0];
$a2=$e[1];
call_user_func("myfunc",$a1,$a2);
+2

call_user_func_array, :

call_user_func_array($exe, $pars);

eval ( ):

eval("do_lightbox('image1.jpg', 'picture 1')");

.

+1

, , eval:

eval("do_lightbox('image1.jpg', 'picture 1')")
+1

, ( ), :

$exe = "do_lightbox";
$pars = "'image1.jpg', 'picture 1'";
call_user_func_array($exe, explode(',', $pars));

.

+1

All Articles