用PHP翻译字母表

时间:2011-03-07 10:03:18

标签: php translation

我是一个学生对PHP感到困惑.... 这是我们的功课:

两个孩子创造了自己的语言。当他们写作时,真的很难理解。 你的目的是翻译他们的话来理解他们。 当他们写杏时,它想说海豚

修改文件index.php并声明一个类似«a.p.r.i.c.o.t»的字符串和一个包含关联数组的变量。 编写一个返回翻译后的单词并显示单词的函数。 您必须使用循环和两个函数:implode()explode()。 !

5 个答案:

答案 0 :(得分:1)

我假设您具有PHP语法的基本知识。

implode($ glue,$ pieces)是一个函数,它接受一个数组($ pieces)并将所有部分放在一起作为字符串。所以:

<?php
$pieces[0] = 'This';
$pieces[1] = 'is';
$pieces[2] = 'a';
$pieces[3] = 'sentence.';
?>

当加入implode('',$ pieces)时会返回字符串'Thisisasentence'。第一个参数($ glue)是单词之间的分隔符,所以我们可以使用空格(例如implode('',$ pieces))并获得'这是一个句子。'

explode($ delimiter,$ string)以相反的方式工作。也就是说它会将字符串转换为数组。 e.g。

<?php
$pieces[0] = 'This';
$pieces[1] = 'is';
$pieces[2] = 'a';
$pieces[3] = 'sentence.';

$str = implode(' ', $pieces);

$pieces2 = explode(' ', $str);
# $pieces2 is now the same as $pieces.
?>

然后内爆。我不会给你PHP,因为你想自己做,但这是伪代码:

Explode string into words.
Loop through array.
  If word is 'apricot'.
    Change word to 'dolphin'.
Implode array.

答案 1 :(得分:0)

看起来你在谈论Ceaser的密码,google it,我在Wiki上看到了一个Java示例链接,可以安全地转换为PHP

答案 2 :(得分:0)

您可以从制作数组开始:

$letters = array();
$letters['a'] = 'd';
$letters['p'] = 'o'; //etc till all keys are 'apricot' and the values are 'dolphin'

之后,您必须查找有关implode的一些信息并爆炸,我将考虑作弊再次提供帮助..

答案 3 :(得分:0)

应该这样做......

<?php 

// Declare string a.p.r.i.c.o.t in var
$string_in = 'a.p.r.i.c.o.t';

// Declare translate function
function translate($string) {

    // Create translation map
    $map = array(
        'a' => 'd',
        'p' => 'o',
        'r' => 'l',
        'i' => 'p',
        'c' => 'h',
        'o' => 'i',
        't' => 'n'
    );

    // Set new output array
    $tmp_out = array();

    // Transform string in array with explode
    $tmp_in = explode('.', $string);

    // Loop on apricot array
    foreach ($tmp_in as $key => $value) {
        $tmp_out[] = $map[$value];
    }

    // return output array as string with implode
    return implode('.', $tmp_out);
}

// This translates 'a.p.r.i.c.o.t' to 'd.o.l.p.h.i.n'
echo translate($string_in);


?>

答案 4 :(得分:0)

有许多(希望)有用的评论来解释每一行的作用

//  Set up a "transposition" table of letters,
//      showing what each letter should become
$letterLookup = array(  
        'a' => 'd',     'b' => 'a',     'c' => 'h',     'd' => 'b',
        'e' => 'c',     'f' => 'e',     'g' => 'f',     'h' => 'g',
        'i' => 'p',     'j' => 'j',     'k' => 'k',     'l' => 'm',
        'm' => 'q',     'n' => 'r',     'o' => 'i',     'p' => 'o',
        'q' => 's',     'r' => 'l',     's' => 't',     't' => 'n',
        'u' => 'u',     'v' => 'v',     'w' => 'w',     'x' => 'x',
        'y' => 'y',     'z' => 'z'
);

//  This is the initial input string
$inputString = 'a.p.r.i.c.o.t';

//  Display our initial input string (just for test purposes)
echo $inputString,'<hr />';


//  Convert the input string into an array, so we can loop through each
//      letter more easily, splitting on the dots
$stringArray = explode('.',$inputString);
//  Initialise the new array that we're going to create in out new
//      language/code
$newStringArray = array();
//  Loop through each letter from the input string one after the other
//      (easy with an array)
foreach($stringArray as $letter) {
    //  If the letter is in our transposition table...
    if (isset($letterLookup[$letter])) {
        //    then add that new letter to our new
        //        language/code array
        $newStringArray[] = $letterLookup[$letter];
    } else {
        //    Otherwise (if it's a punctuation mark, for example)
        //        add that to our new language/code array
        //        without changing it
        $newStringArray[] = $letter;
    }
}


//  Change our translated/encoded array back into a string,
//      putting the dots back in
$newString = implode('.',$newStringArray);

//  Then display our new string
echo $newString;