gpt4 book ai didi

c# - PHP 相当于 C# 字符串格式化

转载 作者:行者123 更新时间:2023-12-03 03:33:18 25 4
gpt4 key购买 nike

我正在尝试复制我的 C# 字符串格式,我可以执行如下操作:

Console.Writeline("My name is [0].  I have been a [1] for [2] days.", "bob", "member", "12")

我希望能够在 PHP 中执行此操作,但据我所知,唯一与此类似的函数是 sprintf()。有没有和上面的功能一样的

echo function("My name is [0].  I have been a [1] for [2] days.", "bob", "member", "12")

最佳答案

您可以编写自己的函数,例如

<?php
function Format($format /*, ... */) {
$args = func_get_args();
return preg_replace_callback('/\[(\\d)\]/',
function($m) use($args) {
// might want to add more error handling here...
return $args[$m[1]+1];
},
$format
);
}

$x = 'a';
$y = 'b';
echo Format('1=[1], 0=[0]', $x, $y);

关于c# - PHP 相当于 C# 字符串格式化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7142077/

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