1 回答
TA贡献1847条经验 获得超11个赞
您要遍历数组,然后使用数组的索引对需要替换的值进行字符串替换:
<?php
$lang = '{1} say {2} {3} {4}';
$param = array(
'1' => 'aaa',
'2' => 'bbb',
'3' => 'ccc',
'4' => 'ddd'
);
foreach($param as $index => $p) {
$lang = str_replace('{' . $index . '}', $p, $lang);
}
echo $lang; //aaa say bbb ccc ddd
就个人而言,我会使用方括号...
<?php
$lang = '[1] say [2] [3] [4]';
$param = array(
'1' => 'aaa',
'2' => 'bbb',
'3' => 'ccc',
'4' => 'ddd'
);
foreach($param as $index => $p) {
$lang = str_replace("[$index]", $p, $lang);
}
echo $lang; //aaa say bbb ccc ddd
您可以避免随后断开字符串。
使用字符串索引和数字:
<?php
$lang = '{char_1} say {2} {char_3} {4}';
$param = array(
'char_1' => 'aaa',
'2' => 'bbb',
'char_3' => 'ccc',
'4' => 'ddd'
);
foreach($param as $index => $p) {
$lang = str_replace('{' . $index . '}', $p, $lang);
}
echo $lang; //aaa say bbb ccc ddd
- 1 回答
- 0 关注
- 161 浏览
添加回答
举报