我需要一个函数将单词中的每个字母替换为其他字母。例如:
a = tu
b = mo
c = jo
如果我写"abc",我想得到"tumoji",如果我写"bca",我想得到"mojotu",等等
$from = array('a',
'b',
'c'
);
$to = array('tu',
'mo',
'jo'
);
$original = 'cab';
$new = strtr($original,$from,$to);
或
$replacements = array('a' => 'tu',
'b' => 'mo',
'c' => 'jo'
);
$original = 'cab';
$new = strtr($original,$replacements);
或
$replacements = array('a' => 'tu',
'b' => 'mo',
'c' => 'jo'
);
$original = 'cab';
$new = '';
foreach(str_split($original) as $letter) {
$new .= $replacements[$letter];
}
使用strtr()
$str = strtr($str, array('a' => 'tu' /*, ... */));