dongxiansi0158 2011-03-04 13:51
浏览 43
已采纳

在PHP中将字符转换为另一个字符

I have two arrays one with fake Japanese characters, another with the English alphabet I have no idea where to go from here, I've tried loops, str_replace, even using the letters array as the keys for the jap array which did work for one word, but I want to break up the words and convert them while including the space.

$name = $_POST['engname'];
$name = strtoupper($name);

$jap = array('ka','tu','mi', 'te','ku', 'lu', 'ji', 'ri', 'ki', 'zu', 'me', 'ta', 'rin', 'to', 'mo', 'no', 'ke', 'shi', 'ari', 'chi', 'do', 'ru', 'mei', 'na', 'fu', 'zi');
$letters = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');

$names = explode(' ',$name);
$letters = array();
foreach($name as $names) {
$names[] = join('<br/>', str_split($names));
}
echo join('<br/>',$names);
  • 写回答

2条回答 默认 最新

  • duanqiu2064 2011-03-04 13:57
    关注

    PHP has a function for that: strtr

    strtr - Translate characters or replace substrings

    If given two arguments, the second should be an array in the form array('from' => 'to', ...). The return value is a string where all the occurrences of the array keys have been replaced by the corresponding values. The longest keys will be tried first. Once a substring has been replaced, its new value will not be searched again.

    $name = strtr($name, array_combine($letters, $jap));
    

    (Not sure in which direction you want to go, JAP->ENG or ENG->JAP but from the fact that you use strtoupper I assume the latter)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制