Variable substitution in php SMS template?
伊谢尔伦
伊谢尔伦 2017-05-16 13:14:08
0
3
363

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?

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(3)
刘奇

str_replace()

洪涛

Why eval is needed? It’s not needed at all

Peter_Zhu
<?php
    $name = 'Panda';
    $code = 6379;
    printf("欢迎注册%s,你的注册码是:%d",$name,$code);
    //欢迎注册Panda,你的验证码是6379
?>

For details on the printf() function, please see http://php.net/sprintf

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!