leojarina
leojarina

Reputation: 157

Convert String to variable in PHP inside function argument

below is the code I have created. My point here is to convert all strings inside the function argument.

For example, fname('variable1=value1&variable2=value2'); I need to convert variable1 & variable2 into ang variable as $variable1, $variable2 instead parsing a plain text. I found out eval() function is useful but it only takes one string which is the "variable1" and its value.

function addFunction($arg){
    echo eval('return $'. $arg . ';');
}
addFunction('variable1=value1&variable2=value2');

Now the problem is I got this error "Parse error: syntax error, unexpected '=' in D:\xampp\htdocs...\index.php(7) : eval()'d code on line 1". But if I have only one variable and value inside the function argument it works perfect, but I need to have more parameters here. Is this possible to do this thing or is there any other way to count the parameters before it can be evaluated?

Thank you,

Upvotes: 0

Views: 854

Answers (6)

Federkun
Federkun

Reputation: 36924

function addFunction($arg)
{
    parse_str($arg, $args);

    foreach ($args as $key => $val) {
        echo $key , " --> " , $val, "\n";
    }


}

addFunction('variable1=value1&variable2=value2');

Output

variable1 --> value1
variable2 --> value2

You can also use

function addFunction($arg)
{
    parse_str($arg);

    echo $variable2; // value2

}

addFunction('variable1=value1&variable2=value2');

Upvotes: 1

Hackerman
Hackerman

Reputation: 12305

Something like this:

function addFunction($arg)
{
  $varArr = explode("&",$arg);
  $varResponse ="";

  foreach ($varArr as $varKey=>$varVal) {
   $varResponse = $varResponse."$".$varVal.";";
  }
   return $varResponse;
 }
  echo addFunction('variable1=value1&variable2=value2');

Saludos ;)

Upvotes: 1

Husman
Husman

Reputation: 6909

You need split the $arg at & to get each variable and then again at = to get each variable and value.

$arg = 'variable1=value1&variable2=value2';
$vars = explode('&', $arg);

foreach ($vars as $var) {
    $parts = explode("=", $var);
    echo eval('return $'. $parts[0] . '='. $parts[1] . ';');
}

Upvotes: 1

Kristen Waite
Kristen Waite

Reputation: 1465

Perhaps you can use explode()?

$varArr = explode("&",$arg);
foreach ($varArr as $varKey=>$varVal) {
    echo eval('return $'.$varKey.'='.$varVal.';');
}

Upvotes: 1

Ryan
Ryan

Reputation: 28177

You can break a string up using the PHP explode function, and then use eval to evaluate each variable indepedently.

 $myvars = explode ('$', 'variable1=value1&variable2=value2');

$myvars is then an array which you can parse and feed to eval as needed.

Upvotes: 1

BenOfTheNorth
BenOfTheNorth

Reputation: 2872

You are trying to create a variable with this name:

$variable1=value1&variable2=value2

You need to explode it at the & to get just the desired future variable names.

function addFunction($arg){

    echo eval('return $'. $arg . ';');
}

$string = 'variable1=value1&variable2=value2';

$array = explode('&', $string);

foreach($array as $part)
{
    addFunction($part);
}

Upvotes: 1

Related Questions