Another funny class with no serious use. This is a class I wrote to determine the numerical positions of letters in the English alphabet.
How to use ?
1 2 3 4 5 6 |
<?php include('alphabet.php'); $a = new alphabet(); // construct the alphabet object echo $a->getNum('z'); // outputs 26 echo $a->getChar(3); // outputs 'c' ?> |
As you can see, using the getNum() method, you can feed a letter and get the serial number or numerical position of that letter. Again in the same way, use the getChar() method to input a number and get the character/letter in that position.
I use it very often while breaking alphabet related codes.
Source Codes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php // filename: alphabet.php // Author: maSnun class alphabet { public $num, $alpha,$dict_1,$dict_2; function __construct() { $this->num = array(); // placeholder for 1 to 26 $this->alpha = array(); // placeholder for a to z for($i=1;$i<27;$i++) { $this->num[]= $i; } // dynamically store 1 to 26 in the array for($i='a';$i<'z';$i++) { $this->alpha[]= $i; } // dynamically store a to y in the array $this->alpha[] = 'z'; // manually add z to the array $this->dict_1 = array_combine($this->num,$this->alpha); // combine them to form a dictionary $this->dict_2 = array_combine($this->alpha,$this->num); // combine them to form a dictionary } function getNum($char) { return $this->dict_2[$char]; } function getChar($n) { return $this->dict_1[$n]; } } ?> |
One reply on “A Handy alphabet class :)”
// Do you really need this line?
$this->alpha[] = ‘z’; // manually add z to the array
// You could modify this line
for($i=’a’;$ialpha[]= $i; }
// to
for($i=’a’;$ialpha[]= $i; }
Would that work?