在 PHP 中翻译字母表

发布于 2024-10-20 18:54:38 字数 275 浏览 2 评论 0原文

我是一名学生,对 PHP 感到困惑...... 这是我们的作业:

两个孩子创造了自己的语言。当他们写的时候,真的很难理解。 你的目的是翻译他们的话以理解他们。 当他们写 apricot 时,它想说 dolphin

修改文件 index.php 并声明一个像这样的字符串 « apricot » 和一个包含关联数组的变量。 编写一个返回翻译后的单词并显示该单词的函数。 您必须使用循环和两个函数:implode()explode()。 !

i'm a student confused about PHP....
this is our homework:

Two children created their own language. And when they write it’s really difficult to understand.
Your purpose is to translate their words to understand them.
When they write apricot it wants to say dolphin

Modify the file index.php and declare a string like this « a.p.r.i.c.o.t » and a variable which contains the associative array.
Write a function which returns the translated word and display the word.
You have to use a loop and both functions: implode() and explode().
!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

别挽留 2024-10-27 18:54:38

我假设您有 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) 的工作方式相反。也就是说它将把字符串变成数组。例如

<?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.

I'll assume you have basic knowledge of PHP syntax.

implode($glue, $pieces) is a function that takes an array ($pieces) and puts all the parts together as a string. So:

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

When fed into implode('', $pieces) will return the string 'Thisisasentence.' The first parameter ($glue) is the separator between the words, so we could use a space (e.g. implode(' ', $pieces)) and get 'This is a sentence.'

explode($delimiter, $string) works in the opposite way. That is it will turn the string into an array. 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.
?>

Then implode. I won't give you the PHP since your suppose to be doing it yourself, but here's it in pseudocode:

Explode string into words.
Loop through array.
  If word is 'apricot'.
    Change word to 'dolphin'.
Implode array.
若沐 2024-10-27 18:54:38

这应该可以做到...

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


?>

This should do it...

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


?>
梦罢 2024-10-27 18:54:38

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

It looks like you are talking about Ceaser's cipher, google it, I have seen a Java example link on Wiki which can be safely converted to PHP

Bonjour°[大白 2024-10-27 18:54:38

您可以从创建一个数组开始:

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

之后您必须查找一些有关 implode然后爆炸,再有帮助我会考虑作弊..

You can start by making an array:

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

After that you have to look up some info about implode and explode, anymore help i would consider cheating..

丶视觉 2024-10-27 18:54:38

有很多(希望)有用的注释来解释每一行的作用

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

With lots of (hopefully) helpful comments to explain what each line does

//  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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文