Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP替换单词中的每个字母_Php_Function - Fatal编程技术网

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];
}