Page 1 of 1
Letters to Numbers
Posted: Fri Aug 14, 2009 2:31 pm
by tecktalkcm0391
Hello,
I am having trouble thinking this through logically. How can I convert a string of letters to numbers... for example,
ABC would become 010203, because A = 01, B = 02, and C = 03.
I would like it to be in a function form.
Thanks a ton!
Chris
Re: Letters to Numbers
Posted: Fri Aug 14, 2009 7:36 pm
by tecktalkcm0391
That worked perfectly! Thanks!
Re: Letters to Numbers
Posted: Fri Aug 14, 2009 10:19 pm
by tecktalkcm0391
I was playing around trying to get this to do the opposite, but I'm having no luck. The ASCII numbering is confusing me I think. Any help on reversing this from string to alpha?
Re: Letters to Numbers
Posted: Sat Aug 15, 2009 9:00 am
by tecktalkcm0391
I have this so far, but I'm not getting any letter back, any help please?
Code: Select all
function numbers_to_letters($string){
// Initializes output string
$output = '';
// Sperates numbers into array
preg_match_all('/[0-9]{2}/', $string, $matches);
// Remembers the ASCII number of "A", minus one
// so that "A" becomes "01" instead of "65"
$a = chr(01) - 1;
// Loops through each two digit number in the string
foreach ($matches as $digit) {
// Converts a two-digit number to a string
// starting with "01" => "A"
// and appends it to the output string
$output .= sprintf('%02d', chr($digit) - $a);
}
return $output;
}
Re: Letters to Numbers
Posted: Sat Aug 15, 2009 11:20 am
by tecktalkcm0391
I think I filled in most of the blanks right... can someone check them, and point me in the right direction for the ones I'm missing?
Code: Select all
<?php
function numbers_to_letters ($string){
// Initializes output string
$output = '';
// Removes non-alphabetical characters
/*[...]*/
// Initializes array to hold digit pairs
$matches = array();
// Separates number string into digit pairs array
preg_match_all('/[0-9]{2}/', $string, $matches);
// Remembers ASCII value of first letter of the alphabet
$a = chr(01) + 1;
// Remembers ASCII value of last letter of the alphabet
$z = chr(26) + 1;
// Loops through digit pairs
foreach ($matches[0] as $num) {
// Restores number to ASCII value (01 => 65, 26 => 90)
/*[...]*/
// Checks if number is in alphabetical range
/*[...]*/ {
// Appends letter to output
$output .= chr($num) + $a;
}
}
return $output;
}
?>
Re: Letters to Numbers
Posted: Sat Aug 15, 2009 2:14 pm
by tecktalkcm0391
I'm almost there I'm using this now, but I'm not getting any output. I try doing echo's in the loop, but nothing returns:
Any more help
Code: Select all
function numbers_to_letters ($string)
{
// Initializes output string
$output = '';
// Removes non-digit characters
$string = preg_replace('/[^0-9]/', '', $string);
// Initializes array to hold digit pairs
$matches = array();
// Separates number string into digit pairs array
preg_match_all('/[0-9]{2}/', $string, $matches);
// Debug
return $matches[0];
// Remembers ASCII value of first letter of the alphabet
$a = 01 + 64;
// Remembers ASCII value of last letter of the alphabet
$z = 90 + 64;
// Loops through digit pairs
foreach ($matches[0] as $num) {
// Restores number to ASCII value (01 => 65, 26 => 90)
$num = $num + 64;
// Checks if number is in alphabetical range
if(($a <= $num)&& ($num <= $z)){
// Appends letter to output
$output.= chr($num);
}
}
return $output;
}
Re: Letters to Numbers
Posted: Sat Aug 15, 2009 3:23 pm
by tecktalkcm0391
Thanks for all your help! I got it!
Code: Select all
function numbers_to_letters ($string)
{
// Initializes output string
$output = '';
// Removes non-digit characters
$string = preg_replace('/[^0-9]/', '', $string);
// Initializes array to hold digit pairs
$matches = array();
// Separates number string into digit pairs array
preg_match_all('/[0-9]{2}/', $string, $matches);
// Remembers ASCII value of first letter of the alphabet
$a = ord('A') - 64;
// Remembers ASCII value of last letter of the alphabet
$z = ord('Z') - 64;
// Loops through digit pairs
foreach ($matches[0] as $num) {
// Restores number to ASCII value (01 => 65, 26 => 90)
$num = $num + 64;
// Checks if number is in alphabetical range
if($num >= $a && $num <= $z){
// Appends letter to output
$output.= chr($num);
}
}
return $output;
}
Re: Letters to Numbers
Posted: Sat Aug 15, 2009 6:06 pm
by tecktalkcm0391
Thanks, could you explain why you made the slight changes?