gpt4 book ai didi

PHP变量替换

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:00:16 24 4
gpt4 key购买 nike

是否有任何函数可以替换字符串中的参数?像这样:

代码:

$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 */

$str运算后的期望值是:

My name is Joe.

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

最佳答案

好像strtr什么是可以做同样事情的内建函数。 (通过 drupal 代码得到这个)。

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

关于PHP变量替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/967926/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com