PHP变量替换

Sha*_*eem 4 php

是否有任何函数替换字符串中的params?像这样的东西:

码:

$format_str = "My name is %name."; /* this was set in a 
                                      configuration file - config.php */

$str = xprintf($format_str, array('name' => 'Joe', 'age' => 150)); 
              /* above is somewhere in main code */
Run Code Online (Sandbox Code Playgroud)

操作后$ str的预期值为:

My name is Joe.
Run Code Online (Sandbox Code Playgroud)

更新:我知道sprintf.但是,在这种情况下,这还不够.我修改了代码以显示有什么区别.

Sha*_*eem 10

似乎strtr是一个内置函数,可以做同样的事情.(通过drupal代码得到这个).

>> $format_str = "My name is %name.";
My name is %name.
>> strtr($format_str, array('%name' => 'Joe', '%age' => 150))
My name is Joe.
Run Code Online (Sandbox Code Playgroud)