aximbigfan Posted November 28, 2008 Share Posted November 28, 2008 Ok, so backstory: I had originally posted asking how a function would work, to convert a string into a number, like below: a = 1 b = 2 z = 26 aa = 27 zz = 702 aaa = 703 etc... The trick, is that I cannot simply keep incrementing the string in a loop, until it gets the input, and returning how many iterations have run. It needs to be in a "single action", liek below: function let2num3($input) { $inputarray = str_split($input); $inputarray = array_reverse($inputarray, false); $sumT = 0; $i = 1; $range = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5, 'f' => 6, 'g' => 7, 'h' => 8, 'i' => 9, 'j' => 10, 'k' => 11, 'l' => 12, 'm' => 13, 'n' => 14, 'o' => 15, 'p' => 16, 'q' => 17, 'r' => 18, 's' => 19, 't' => 20, 'u' => 21, 'v' => 22, 'w' => 23, 'x' => 24, 'y' => 25, 'z' => 26 ); foreach ($inputarray as $key=>$val) { if ($i==1) $sumL = $range[$val]; else $sumL = 26 * $range[$val]; $sumT = $sumT + $sumL; $i++; } return $sumT; } This issue is, that it works up to zz perfectly, but past hat it doesn't return the right number. Help? Thanks, Chris Quote Link to comment https://forums.phpfreaks.com/topic/134587-string-to-int-redux/ Share on other sites More sharing options...
DarkWater Posted November 28, 2008 Share Posted November 28, 2008 EDIT: Tell me again how you're getting aaa to be 703? Quote Link to comment https://forums.phpfreaks.com/topic/134587-string-to-int-redux/#findComment-700768 Share on other sites More sharing options...
aximbigfan Posted November 28, 2008 Author Share Posted November 28, 2008 Is it on less, or one more? I am using this function to verify that my "single action" function is correct. function slow($input) { $string = 'a'; $i = 1; while ($string != $input) { $string++; $i++; } return $i; } When aaa is run through it, it outputs 703. Thanks! Chris Quote Link to comment https://forums.phpfreaks.com/topic/134587-string-to-int-redux/#findComment-700780 Share on other sites More sharing options...
aximbigfan Posted November 28, 2008 Author Share Posted November 28, 2008 NVM, I got it!!!!!! 4 days of work... Into this... function let2num3($input) { $inputarray = str_split($input); $inputarray = array_reverse($inputarray, false); $sumT = 0; $i = 0; $range = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5, 'f' => 6, 'g' => 7, 'h' => 8, 'i' => 9, 'j' => 10, 'k' => 11, 'l' => 12, 'm' => 13, 'n' => 14, 'o' => 15, 'p' => 16, 'q' => 17, 'r' => 18, 's' => 19, 't' => 20, 'u' => 21, 'v' => 22, 'w' => 23, 'x' => 24, 'y' => 25, 'z' => 26 ); foreach ($inputarray as $key=>$val) { $sumL = pow(26, $i) * $range[$val]; $sumT = $sumT + $sumL; $i++; } return $sumT ; } Chris Quote Link to comment https://forums.phpfreaks.com/topic/134587-string-to-int-redux/#findComment-700784 Share on other sites More sharing options...
rhodesa Posted November 28, 2008 Share Posted November 28, 2008 this is what i got: <?php function let2num3($input) { $letters = range('a','z'); $total = 0; foreach(array_reverse(str_split($input)) as $n=>$letter){ $sum = array_search($letter,$letters) + 1; if($n) $sum = (pow(26,$n) * $sum); $total += $sum; } return $total; } print let2num3('aa'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/134587-string-to-int-redux/#findComment-700788 Share on other sites More sharing options...
rhodesa Posted November 28, 2008 Share Posted November 28, 2008 yours + mine got me this simpler version: function let2num3($input) { $letters = range('a','z'); $sum = 0; foreach(array_reverse(str_split($input)) as $i=>$letter) $sum += pow(26, $i) * (array_search($letter,$letters) + 1); return $sum; } print let2num3('zz'); Quote Link to comment https://forums.phpfreaks.com/topic/134587-string-to-int-redux/#findComment-700791 Share on other sites More sharing options...
sasa Posted November 28, 2008 Share Posted November 28, 2008 or <?php function str2int($in){ $out = round(pow(26, strlen($in))/25); $l = range('a', 'z'); $l = array_flip($l); $tmp = 0; for ($i = 0; $i<strlen($in); $i++){ $tmp = $tmp * 26 + $l[$in[$i]]; } return $out + $tmp; } function int2str($in){ $l = range('a', 'z'); $len = 0; $base = 1; while ($in >= $base){ $len++; $in -= $base; $base *= 26; } $out = ''; for ($i = 0; $i < $len; $i++){ $out = $l[$in % 26]. $out; $in = (int) $in / 26; } return $out; } echo int2str(335115), "\n"; echo str2int('zz') ?> Quote Link to comment https://forums.phpfreaks.com/topic/134587-string-to-int-redux/#findComment-700867 Share on other sites More sharing options...
Mark Baker Posted November 28, 2008 Share Posted November 28, 2008 Unless I'm missing something, these still all seem long and complex using ranges of A-Z values and powers and reversing the string: <?php function columnIDToColumnIndex($columnID) { $columnIndex = 0; foreach(str_split(strtoupper($columnID)) as $value) { $columnIndex = $columnIndex * 26 + (ord($value) - 64); } return $columnIndex; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/134587-string-to-int-redux/#findComment-700883 Share on other sites More sharing options...
aximbigfan Posted November 28, 2008 Author Share Posted November 28, 2008 Thanks Sasa, I did need an int to str function! Chris Quote Link to comment https://forums.phpfreaks.com/topic/134587-string-to-int-redux/#findComment-701323 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.