0

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,

6 Answers 6

1
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');
Sign up to request clarification or add additional context in comments.

Comments

1

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);
}

Comments

1

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.

Comments

1

Perhaps you can use explode()?

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

Comments

1

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] . ';');
}

Comments

1

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 ;)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.