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条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效