2016-12-19 12 views
0

文字列のプレースホルダのセットを部分文字列の配列で置き換えるにはどうすればよいですか?(PHP)文字列内の異なる部分文字列を値の配列で置き換えるにはどうすればいいですか?

マイコード:

$replaceWith = array('FIAT', 'car'); 
$message = '{%s} is the best {%s} of the world.'; 
$finalMessage = str_replace(array_fill(0, count($replaceWith), '{%s}'), $replaceWith, $message); 

var_dump($finalMessage); 

出力:

string 'FIAT is the best FIAT of the world.' (length=35) 

所望の出力:事前に

string 'FIAT is the best car of the world.' (length=34) 

Thks!

答えて

1

は、以下試してみてください。

<?php 
$subject = '%s is the best %s of the world.'; 
$values = array('FIAT', 'car'); 
echo vsprintf($subject, $values); 
?> 
+0

TKS多く、スーマン・シン! – random425

関連する問題