2009-10-21 67 views
1

此函数numtoalpha如何输出大于9的值的字母等效值? 结果,这样的事情:10为A,11为B等....如何将数字转换为PHP中的字母?

PHP.net甚至没有该功能,或者我没有看正确的地方,但我确定它说功能。

<?php 
$number = $_REQUEST["number"]; 
/*Create a condition that is true here to get us started*/ 
if ($number <=9) 
{ 
echo $number; 
} 
elseif ($number >9 && $number <=35) 
{ 
echo $number; 
function numtoalpha($number) 
{ 
echo $number; 
} 
echo"<br/>Print all the numbers from 10 to 35, with alphabetic equivalents:A for10,etc"; 
?> 

回答

8

你需要使用base_convert

$number = $_REQUEST["number"]; # '10' 
base_convert($number, 10, 36); # 'a' 
+0

不知道有关base_convert,感谢很酷的功能。 – Newb 2009-10-22 03:47:24

0

试试这个:

<?php 

    $number = $_REQUEST["number"]; 

    for ($i=0;$i<length($number);$i++) { 
    echo ord($number[$i]); 
    } 

?> 

这会给你相应的字符的ASCII码。 55削弱它,你会得到10 A,11对于B等...

5

你基本上会做一些数学来生成所需的值正确的ASCII码。

所以:

if($num>9 && $num<=35) { 
echo(chr(55+$num)) 
} 
0

使用下面的函数。

function patient_char($str) 
{ 
$alpha = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X ', 'Y', 'Z'); 
$newName = ''; 
do { 
    $str--; 
    $limit = floor($str/26); 
    $reminder = $str % 26; 
    $newName = $alpha[$reminder].$newName; 
    $str=$limit; 
} while ($str >0); 
return $newName; 
} 
相关问题