说我这样做:
$string = 'some string with a {$var}';
// Set var after
$var = 'variable'
// String with just set var
$newString = 'Here is my string: ' . $string;
echo $newString;
我该如何让它输出:
Here is my string: some string with a variable
我可以用
str_replace()
做到这一点,但我想知道是否有更简单的方法?
sprintf()
适合您:
$string = 'some string with a %s';
$var = 'variable';
$newString = sprintf($string, $var);