For example: $str = 'Welcome to register xxx, your registration code is: {$code}';
When calling, first generate $code = 123456;
How to replace $code
in $str
with 123456
, the result is: Welcome to register xxx, your registration code is: 123456
Solution 1:
$code = 123456;
$str = '欢迎注册xxx,你的注册码是:{$code}';
eval("$str = \"$str\";");
Are there any other elegant solutions?
str_replace()
Why eval is needed? It’s not needed at all
For details on the printf() function, please see http://php.net/sprintf