简体   繁体   English

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

[英]Converting a character to another character in 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. 我有两个数组,一个带有假日文字符,另一个带有英语字母,我不知道从这里去哪里,我试过循环,str_replace,甚至使用字母数组作为jap数组的键也可以。单词,但我想分解单词并在包含空格的情况下将其转换。

$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);

PHP has a function for that: strtr PHP具有此功能: strtr

strtr - Translate characters or replace substrings strtr翻译字符或替换子字符串

If given two arguments, the second should be an array in the form array('from' => 'to', ...) . 如果给定两个参数,则第二个参数应为array('from' => 'to', ...)形式的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) (不确定您要朝哪个方向前进,JAP-> ENG或ENG-> JAP,但是基于您使用strtoupper的事实,我认为是后者)

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

$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');

$name = str_replace( $jap , $letters , $name );

echo $name;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM