PHP替换单词中的每个字母
我需要一个函数来将单词中的每个字母替换为其他字母。例如:PHP替换单词中的每个字母,php,function,Php,Function,我需要一个函数来将单词中的每个字母替换为其他字母。例如: a = tu b = mo c = jo 如果我写“abc”,我想得到“tumoji”,如果我写“bca”,我想得到“mojotu”,等等。使用 或 或 您确定在第一个示例中正确使用了strtr()吗?您是否应该传递array\u combine($from,$to)?@alex-from/to和replacements都是有效语法,而且肯定可以组合from/to数组集来生成replacements数组。。。不过,首先最好将其定义为关联
a = tu
b = mo
c = jo
如果我写“abc”,我想得到“tumoji”,如果我写“bca”,我想得到“mojotu”,等等。使用
或
或
您确定在第一个示例中正确使用了
strtr()
吗?您是否应该传递array\u combine($from,$to)
?@alex-from/to和replacements都是有效语法,而且肯定可以组合from/to数组集来生成replacements数组。。。不过,首先最好将其定义为关联替换数组
$str = strtr($str, array('a' => 'tu' /*, ... */));
$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];
}