aximbigfan Posted November 26, 2008 Share Posted November 26, 2008 I need a function that will receive an int, and transform it into a letter representation. PHP can do a neat thing, where it will increment a string, as so... a b c d ... z aa ab ac ad . What I need to do, is convert this into an int. For example, aa would be 27, ab would be 28, a would be 1, b would be 2, you get it. I also need to convert a number into this kind of representation. Any ideas? Thanks, Chris Quote Link to comment https://forums.phpfreaks.com/topic/134339-int-into-string/ Share on other sites More sharing options...
Adam Posted November 26, 2008 Share Posted November 26, 2008 Quite complicated if you ask me, would take some thinking power! This topic may be better suited to the freelance section? Unless someone's done this before? I'll keep thinking but every idea I have at the minute is a dead end! Sorry pal! Adam Quote Link to comment https://forums.phpfreaks.com/topic/134339-int-into-string/#findComment-699383 Share on other sites More sharing options...
Mchl Posted November 26, 2008 Share Posted November 26, 2008 Just use str_replace [edit] Wrong But you could use ord and chr Quote Link to comment https://forums.phpfreaks.com/topic/134339-int-into-string/#findComment-699388 Share on other sites More sharing options...
unkwntech Posted November 26, 2008 Share Posted November 26, 2008 @Mchl - that would require quite a lot of code as you would need one for each possible element. Quote Link to comment https://forums.phpfreaks.com/topic/134339-int-into-string/#findComment-699390 Share on other sites More sharing options...
Mchl Posted November 26, 2008 Share Posted November 26, 2008 ord('a') = 97 ord('a') - 96 = 1 ord('b') - 96 = 2 ... ord($char) - 96 = $number chr(1+96) = 'a' chr($number + 96) = $char Quote Link to comment https://forums.phpfreaks.com/topic/134339-int-into-string/#findComment-699392 Share on other sites More sharing options...
unkwntech Posted November 26, 2008 Share Posted November 26, 2008 Nice answer Mchl.... I didn't even know there was a ord() function....... Quote Link to comment https://forums.phpfreaks.com/topic/134339-int-into-string/#findComment-699394 Share on other sites More sharing options...
Mchl Posted November 26, 2008 Share Posted November 26, 2008 I didn't even know there was a ord() function....... It exists in most programming languages (may be named differently) Quote Link to comment https://forums.phpfreaks.com/topic/134339-int-into-string/#findComment-699396 Share on other sites More sharing options...
unkwntech Posted November 26, 2008 Share Posted November 26, 2008 True, but I guess I never needed it in PHP. Quote Link to comment https://forums.phpfreaks.com/topic/134339-int-into-string/#findComment-699399 Share on other sites More sharing options...
sasa Posted November 26, 2008 Share Posted November 26, 2008 try <?php $text = 'u'; for ($i = 1; $i < 10; $i++) echo $text++,"\n"; ?> use ++ operator Quote Link to comment https://forums.phpfreaks.com/topic/134339-int-into-string/#findComment-699426 Share on other sites More sharing options...
aximbigfan Posted November 26, 2008 Author Share Posted November 26, 2008 try <?php $text = 'u'; for ($i = 1; $i < 10; $i++) echo $text++,"\n"; ?> use ++ operator That's perfect! Thanks Sasa! Chris Quote Link to comment https://forums.phpfreaks.com/topic/134339-int-into-string/#findComment-699600 Share on other sites More sharing options...
Mark Baker Posted November 26, 2008 Share Posted November 26, 2008 From PHPExcel: public static function columnIndexFromString($pString = 'A') { // Convert to uppercase $pString = strtoupper($pString); // Convert column to integer if (strlen($pString) == 1) { return (ord($pString{0}) - 64); } elseif (strlen($pString) == 2) { return $result = ((1 + (ord($pString{0}) - 65)) * 26) + (ord($pString{1}) - 64); } elseif (strlen($pString) == 3) { return ((1 + (ord($pString{0}) - 65)) * 676) + ((1 + (ord($pString{1}) - 65)) * 26) + (ord($pString{2}) - 64); } else { throw new Exception("Column string index can not be " . (strlen($pString) != 0 ? "longer than 3 characters" : "empty") . "."); } } public static function stringFromColumnIndex($pColumnIndex = 0) { // Convert column to string $returnValue = ''; // Determine column string if ($pColumnIndex < 26) { $returnValue = chr(65 + $pColumnIndex); } else { $iRemainder = (int)($pColumnIndex / 26) -1; $returnValue = PHPExcel_Cell::stringFromColumnIndex($iRemainder).chr(65 + $pColumnIndex%26) ; } // Return return $returnValue; } Quote Link to comment https://forums.phpfreaks.com/topic/134339-int-into-string/#findComment-699634 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.