search

Home  >  Q&A  >  body text

Variable substitution in php SMS template?

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?

伊谢尔伦伊谢尔伦2871 days ago478

reply all(3)I'll reply

  • 怪我咯

    怪我咯2017-05-16 13:16:08

    str_replace()

    reply
    0
  • PHP中文网

    PHP中文网2017-05-16 13:16:08

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

    reply
    0
  • 天蓬老师

    天蓬老师2017-05-16 13:16:08

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

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

    reply
    0
  • Cancelreply