$my_alphabet = "T";
The above character "T" should print the exact position/number of the alphabet. i.e 20
So, if
$my_alphabet = "A" ;
How can I achieve that.
I see converting number to alphabet .. but the reverse is not there anywhere.
you should be careful about (uppercase, lowercase):
<?php
$upperArr = range('A', 'Z') ;
$LowerArr = range('a', 'z') ;
$myLetter = 't';
if(ctype_upper($myLetter)){
echo (array_search($myLetter, $upperArr) + 1);
}else{
echo (array_search($myLetter, $LowerArr) + 1);
}
?>
By using the ascii value:
ord(strtoupper($letterOfAlphabet)) - ord('A') + 1
in ASCII the letters are sorted by alphabetic order, so...
In case the alphabet letter is not upper case, you can add this line of code to make sure you get the right position of the letter
$my_alphabet = strtoupper($my_alphabet);
so if you get either 'T'
or 't'
, it will always return the right position.
Otherwise @bwoebi's answers will perfectly do the job
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With