我正在尝试复制我的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);
您可以在字符串中内联变量:
$frob = "John";
$foo = "Hello $frob!";