PHP中的嵌套(多个)字符串插值

时间:2021-12-01 21:44:38

If I have a variable that contains nested variables, e.g.:

如果我有一个包含嵌套变量的变量,例如:

$message = "Hello $user_name, an email was send to $user_email ...";
$user_name = 'User Name';
$user_email = 'user@email.com';

is it possible to produce an output such as:

是否有可能产生如下输出:

Hello User Name, an email was send to user@email.com ...

without calling eval()?

没有调用eval()?

4 个解决方案

#1


You can 'define' placeholders and replace it where you need it.

您可以“定义”占位符并将其替换为您需要的位置。

$message = "Hello #user_name#, an email was send to #user_email# ...";
$user_name = 'User Name';
$user_email = 'user@email.com';

$newMessage = str_replace(array("#user_name#", "#user_email#"), array($user_name, $user_email), $message);

See str_replace for reference.

请参阅str_replace以供参考。

#2


Yes it's possible just place the variables $user_email and $user_name above $message so they become instantiated first.

是的,可以将变量$ user_email和$ user_name置于$ message之上,以便首先实例化它们。

$user_email = 'user@email.com';
$user_name = 'User Name';
$message = "Hello $user_name, an email was send to $user_email ...";

echo $message; //Will output: Hello User Name, an email was send to user@email.com ...

EDIT: After reading your reaction you could use a closure for example:

编辑:阅读您的反应后,你可以使用一个闭包,例如:

$message = function($name = null, $email = null){
  return "Hello $name, an email was send to $email ...";
};

$user_name = 'User Name';
$user_email = 'user@email.com';

$newMessage = $message($user_name, $user_email);

#3


Using sprintf or printf function:

使用sprintf或printf函数:

$message = "Hello %s, an email was send to %s ...";
$user_name = 'User Name';
$user_email = 'user@email.com';

echo sprintf($message, $user_name, $user_email);

or

printf($message, $user_name, $user_email);

#4


$user_email = 'user@email.com';
$user_name = 'User Name';
$message = "Hello " . $user_name . ", an email was send to " . $user_email . " ...";

#1


You can 'define' placeholders and replace it where you need it.

您可以“定义”占位符并将其替换为您需要的位置。

$message = "Hello #user_name#, an email was send to #user_email# ...";
$user_name = 'User Name';
$user_email = 'user@email.com';

$newMessage = str_replace(array("#user_name#", "#user_email#"), array($user_name, $user_email), $message);

See str_replace for reference.

请参阅str_replace以供参考。

#2


Yes it's possible just place the variables $user_email and $user_name above $message so they become instantiated first.

是的,可以将变量$ user_email和$ user_name置于$ message之上,以便首先实例化它们。

$user_email = 'user@email.com';
$user_name = 'User Name';
$message = "Hello $user_name, an email was send to $user_email ...";

echo $message; //Will output: Hello User Name, an email was send to user@email.com ...

EDIT: After reading your reaction you could use a closure for example:

编辑:阅读您的反应后,你可以使用一个闭包,例如:

$message = function($name = null, $email = null){
  return "Hello $name, an email was send to $email ...";
};

$user_name = 'User Name';
$user_email = 'user@email.com';

$newMessage = $message($user_name, $user_email);

#3


Using sprintf or printf function:

使用sprintf或printf函数:

$message = "Hello %s, an email was send to %s ...";
$user_name = 'User Name';
$user_email = 'user@email.com';

echo sprintf($message, $user_name, $user_email);

or

printf($message, $user_name, $user_email);

#4


$user_email = 'user@email.com';
$user_name = 'User Name';
$message = "Hello " . $user_name . ", an email was send to " . $user_email . " ...";