开发者

Converting a character to another character in PHP

开发者 https://www.devze.com 2023-02-14 16:15 出处:网络
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 fo

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


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)


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

精彩评论

暂无评论...
验证码 换一张
取 消