从 Array 翻译字符串,str_replace?

发布于 2024-10-24 20:16:20 字数 925 浏览 4 评论 0原文

从 Array 翻译字符串,str_replace?

我有一个数组

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

,我定义为要翻译的常量:

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

不好的方法:

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

我尝试输出(好方法):

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

但我想要的是输出应该是:

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

如何传递 $currency。到str_replace

任何人都可以帮助我做到这一点。

String translate from Array , str_replace?

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.?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倥絔 2024-10-31 20:16:20

我不知道你到底想做什么,但如果它只是输出
尝试

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

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"]);
冷了相思 2024-10-31 20:16:20

好吧,下面只是一个解析器,用于执行您想要的操作。尝试看看它是否适合您的需求:

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;

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;
紫南 2024-10-31 20:16:20

尝试一下:

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

try that:

foreach ($money as $key => $cur)
  echo $cur.' '. $key;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文