0

Is there any way to force a string to evaluate (as a double quoted string/heredoc would)?

For example is there some clean way of doing this:

<?php
$mystring = <<<'MS'
hello {$adjectives['first']} world
MS;

$adjectives = array('first'=>'beautiful');

// here I want to print 'hello beautiful world' 
// instead of 'hello {$adjectives['first']} world'
echo evaluate($mystring); // evaluate is not a real function
?>
6
  • Can you just define $mySytring after $adjectives has been defined? Commented Jan 27, 2015 at 17:16
  • @JuanMendes No, otherwise this would be trivial. I would use a heredoc or double quoted string instead. The point is I want a string evaluated at a later point in time. Commented Jan 27, 2015 at 17:17
  • That's my point, why don't you explain why you need it? Commented Jan 27, 2015 at 17:18
  • @JuanMendes It's necessary for code-reuse and readability purposes. The actual string I want evaluated is very very long and I don't want to put a heredoc everywhere I need it evaluated. The alternate method is of course, using php's include or require so the code doesn't get convoluted, but I was just wondering if there was a better way of doing that. Commented Jan 27, 2015 at 17:22
  • 3
    Instead of using heredoc, use a normal string with placeholders for sprintf? Commented Jan 27, 2015 at 17:23

3 Answers 3

1

You can use eval since you plan to use it only on strings you created your self. Don't ever use eval if the string (or the replacements) are outside your control.

$mystring = <<<'MS'
hello %s world
MS;

$adjectives = array('first'=>'beautiful');

eval('$parsed = ' . json_encode($mystring) . ';');
echo($parsed);

See http://sandbox.onlinephpfunctions.com/code/b1f6afc24efbc685f738dc1e7fd3668afdf5b7d0

As suggested by NATH, sprintf would do the job for you without the security implications of eval

$mystring = <<<'MS'
hello %s world
MS;

$adjectives = array('first' => 'beautiful');
echo sprintf($mystring, $adjectives['first']);
Sign up to request clarification or add additional context in comments.

1 Comment

Please don't promote the use of this function without mentioning how potentially dangerous it can be without proper caution on the input.
1

I would strongly advise to avoid using eval(). I think it's dangerous, slow, and a bad practice in general. Using vsprintf() instead should do the trick for you.

// Use argument swapping (%1\$s instead of %s) to explicitly specify which
// position in the array represents each value. Useful if you're swapping out
// multiple values.
$mystring = <<<MS
hello %1\$s world
MS;

$adjectives = array('first'=>'beautiful');

echo vsprintf($mystring, $adjectives);

Comments

0

Yes and you nearly got it.

Look at this example for eval @ php.net

<?php
$string = 'cup';
$name = 'coffee';
$str = 'This is a $string with my $name in it.';
echo $str. "\n";
eval("\$str = \"$str\";");
echo $str. "\n";
?>

Please note however, that using eval is dangerous and opens the possibility of all kind of attacks if not handled properly.

Maybe a better solution would be to use place holders of some kind.

$str = "This is a __first__ world";

$adjectives = array('first'=>'beautiful');

foreach ($adjectives as $k=>$v) {
   $str = preg_replace('/__'.$k.'__/', $v, $str); 
}

echo $str;

4 Comments

Note that this won't work if the string contains both quotes and double quotes You have to escape them sandbox.onlinephpfunctions.com/code/…
I don't like the idea of using eval. =\
Well its from php.net so... Also ofc it does: $str = 'This is a \'$string\' with \\"my\\" $name in it.'; Works just fine.
@Torge did you click on my example?

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.