开发者

String translate from Array , str_replace?

String translate from Array , str_replace开发者_Python百科?

I have an array

 $money = array(
    "USD"=>100,
    "BAT"=>1000,
    "RIEL"=>2000
    );

And I define as constant to be translate:

define("const_infor","Your __TYPE__ are:  __AMOUNT__  __CURRENCY__ .<br><br>");

Bad WAYS:

  echo "Your balance are :";//more constant here
  foreach ($money as $currency=>$amount){
      echo $money.$currency."; ";
  }

I try to output(GOOD WAYS):

  $tmp1 = "";
  $tmp2 = "";
  foreach ($money as $currency=>$amount){
    $tmp1 .= $money;
    $tmp2 .= $currency;
  }
echo str_replace(ARRAY("__TYPE__","__AMOUNT__","__CURRENCY__"),ARRAY("Balance",$tmp1,$tmp2),const_infor); 

BUT What I want is the output should be :

Your Balance are: 100 USD; 1000 BAT; 2000 RIEL

How can I pass the $currency. to str_replace ?

Anyone can help me to do this.?


I don't know what exactly you wanna do but if it's only output try

printf("Your Money %f %f %f", $money["USD"], $money["BAT"], $money["RIEL"]);


Well, below is just kind of a parser for doing what you want.. Try and see if it fits your needs:

function replace($string, $name = '', $value = '')
{
   if ( !empty($name) )
   {
      str_replace('{'.$name.'}', $value, $string);
   }
}


$string = 'Your balance is {bal1} USD, {bal2} BAT';

$string = replace('bal1', $money['USD'], $string);
$string = replace('bal2', $money['BAT'], $string);
$string = replace('bal3', $money['GBP'], $string);

print $string;


try that:

foreach ($money as $key => $cur)
  echo $cur.' '. $key;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜