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()
.
!
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.
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);
?>
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
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..
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;
精彩评论