从 Array 翻译字符串,str_replace?
从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不知道你到底想做什么,但如果它只是输出
尝试
I don't know what exactly you wanna do but if it's only output
try
好吧,下面只是一个解析器,用于执行您想要的操作。尝试看看它是否适合您的需求:
Well, below is just kind of a parser for doing what you want.. Try and see if it fits your needs:
尝试一下:
try that: