on Basics Tips Tutorials

Programming for kids: PHP string manipulation and arrays

0 comments
code
Tweet about this on TwitterShare on FacebookShare on Google+Share on LinkedInShare on RedditShare on StumbleUpon

As I said earlier in my Programming for kids post, I started to teach my kids some basic programming stuff.

We were playing around with some word games and came up with a simple encrypting trick which I learned when I was a kid.

Basically, you type a word GERMANIKUS and substitute every character in that word with number, starting from 0.

So, G=0, E=1, R=2, M=3, A=4, N=5, I=6, K=7, U=8, S=9. After that, you write your message and substitute every character found in a word GERMANIKUS with a number and all other characters are just plain text.

For example, the word Codeforest would be COD1F1219T. Nice and interesting.

Now, my kids wanted to program a simple script that would take a message as a parameter and encrypt it using the above method. We chose PHP and started with writing down how the program should look and work. My kids were too eager to see the results, we first tried to work this out. This is the first version:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Programming for kids - string replacing</title>
</head>
<body>

<?php
    $secretWord = 'germanikus';
    $message = 'Is this thing working?';
    $key = '';

    $array1 = array('g','e', 'r', 'm', 'a', 'n', 'i', 'k', 'u', 's');
    $array2 = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);

    $encodedMessage = str_replace($array1, $array2, $message);

    echo 'Original message: ' . $message . '<br /><br />';
    echo 'Encoded message: ' . strtoupper($encodedMessage);
?>

</body>
</html>

Demo

Very simple and effective. I explained everything to my kids and they were eager to continue.

Next step was to build a form so you can enter the message to be encrypted.

<form action="" method="POST">
Insert your message:<br />
<textarea name="message" rows="4" cols="60"></textarea><br />
<input type="submit" value="Encode" />
</form>
<br />
		
<?php
    $secretWord = 'germanikus';

    $array1 = array('g','e', 'r', 'm', 'a', 'n', 'i', 'k', 'u', 's');
    $array2 = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);

    if($_POST) {
	$message = strip_tags($_POST['message']);
        $encodedMessage = str_replace($array1, $array2, $message);
	echo 'Encoded message:<br />';
	echo nl2br(strtoupper($encodedMessage));
    }
?>

Demo

Very nice, now we have a basic encrypting mechanism. But what if we want to make this even harder to break? My older kid had an idea: we will allow user to enter a key which will be a number. If a user enters number 3, that would move number 0 (zero) to the third letter of germanikus word.

Great, let’s do it:

<?php
$secretWord = 'GERMANIKUS';
$length = strlen($secretWord);
$secretWordArray = array();
$codeArray = array(0,1,2,3,4,5,6,7,8,9);

// making array of letters out of secretWord
for ($i = 0; $i < $length; $i++) {
	$secretWordArray[$i] = $secretWord[$i];
}

if($_POST) {
	
	$key = strip_tags($_POST['key']);
	// calculating array with our key
	$position = $key[0];
	$number = $key[1];
	$numberOfRotations = 10 - $position;
	for($i = 0; $i < $numberOfRotations; $i++) {
		array_push($codeArray, array_shift($codeArray));
	}
	$count = 0;
	foreach($codeArray as $key => $word) {
		$r[$count] = $secretWord[$key] . ' => ' . $word;
		$count++;	
	}
	echo implode(', ', $r) . '<br />';
	$message = strip_tags($_POST['message']);
	//encode
	$encodedMessage = str_replace($secretWordArray, $codeArray, strtoupper($message));
	//decode
	$decodedMessage = str_replace($codeArray, $secretWordArray, $encodedMessage);
	echo 'Encrypted message:<br />';
	echo nl2br(strtoupper($encodedMessage));
	echo '<br />';
	echo 'Decrypted message:<br />';
	echo nl2br(strtoupper($decodedMessage));
}
?>

Demo

When you try this demo, you will see that if you enter 3 as a key, the fourth letter will be 0. That is ok, as we want to start counting from zero.

That’s it, my kids were delighted with this and have learned a lot about string manipulation and a little bit about arrays in PHP.

Please share your thoughts in comments below.

Tweet about this on TwitterShare on FacebookShare on Google+Share on LinkedInShare on RedditShare on StumbleUpon



Comments are closed.