Convert Number to Letter with PHP

php: converting number to alphabet and vice versa

I don't understand at all the logic you're trying to use in that function. What you're trying to do seems very strange (why does 'a' map to zero and yet 'aa' maps to 26?), but this appears to work. (You will want to use some more test cases, I only checked that it gives the correct output for the case 'aba'.)

function toNum($data) {
$alphabet = 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'
);
$alpha_flip = array_flip($alphabet);
$return_value = -1;
$length = strlen($data);
for ($i = 0; $i < $length; $i++) {
$return_value +=
($alpha_flip[$data[$i]] + 1) * pow(26, ($length - $i - 1));
}
return $return_value;
}

How to convert number to a letter in PHP?

you need to use base_convert:

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

PHP - How to change numbers in a string to letters

Your question does not show any effort, however the following might come useful to you:

It very much depends how long your numbers would be? Assuming 0 to 9, you would do this:

$numbers = array(0,1,2,3,4,5,6,7,8,9);

$number_words = array('zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine');

$string = "I have 3 apples.";

$new_string = str_replace($numbers, $number_words, $string);

The above solution is for simple words and replacements.

For instance, for numbers such as 1995445 you should then search for functions one the internet (or write one) which would convert numbers to string.

Here is a good function to do this:
http://www.karlrixon.co.uk/writing/convert-numbers-to-words-with-php/

What we do, is first extract the number from the string:

$rule = "/([0-9]+)/";
$string = "I have 2 mobile phones, each containing 2500 messages";
$num_match;

Then we loop through the string. Each time we only replace the first occurred number, capture it, pass it to our number_to_string() function and then get the string, use that returned string in our replace function which is preg_replace(). We utilize preg_replace()'s $limit param to limit replacement only to first occurrence on each iteration:

while( preg_match($rule, $string, $num_match) )
{
$string = preg_replace("/".$num_match[0]."/", number_to_string($num_match[0]), $string, 1);
}

echo $string;

What I get in my browser is then:

I have two mobile phones, each containing two thousands and five hundred messages

How to convert integers to characters in PHP

<?php
$list=array('A' => 0,
'B' => 1,
'C' => 2,
'D' => 3,
'E' => 4,
'F' => 5,
'G' => 6,
'H' => 7,
'I' => 8,
'J' => 9);
$num=1234; //your value
$temp='';
$arr_num=str_split ($num);
foreach($arr_num as $data)
{
$temp.=array_search($data,$list);
}
$num=$temp;
echo $num; // here we get BCDE
?>

Convert numeric to letter

See:

  • Convert numbers to letters
  • Converting numbers to words in PHP

To convert to French with Numbers_Words, you do:

// include class
include("Numbers/Words.php");

// create object
$nw = new Numbers_Words();

// convert to string
echo "3000000 in words is " . $nw->toWords(3000000, 'fr');
^
Language Specified

How to convert number to string(letter) in php?

Here is the example....

<form method='POST' action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" >
Enter the Number:
<input type="number" max="999999999" name="number" required="required">(max value:999999999)<br/><input type="submit" value="Submit" name="Submit"><br/></form>
<?php
if(isset($_POST['Submit'])){
class N2L{
var $number = array( 0 => 'zero',1 => 'one',2 => 'two',3 => 'three', 4 => 'four',5 => 'five',6 => 'six',7 => 'seven',
8 => 'eight',9 => 'nine',10 => 'ten',11 => 'eleven',12 => 'twelve',13 => 'thirteen',14 => 'fourteen',
15 => 'fifteen',16 => 'sixteen',17 => 'seventeen',18 => 'eighteen',19 => 'nineteen',20 => 'twenty',
30 => 'thirty',40 => 'forty',50 => 'fifty',60 => 'sixty',70 => 'seventy',80 => 'eighty',90 => 'ninety',
100 => 'hundred',1000 => 'thousand',100000 => 'lakh',10000000 => 'crore');

function index($numb){
if($numb<100){
return $this->upto_99($numb);
}elseif($numb<1000){
return $this->upto_999($numb);
}elseif($numb<100000){
return $this->upto_99999($numb);
}elseif($numb<10000000){
return $this->upto_9999999($numb);
}else{
return $this->upto_999999999($numb);
}
}
function upto_999999999($numb){
$number= $this->number;
if($numb<=200000000){
$crores=(int)($numb/10000000);
$str=" ".$number[$crores]." ".$number[10000000];
}else{
$crores=(int)($numb/100000000)*10;
$crores_odd=(int)($numb%100000000)/10000000;
$str=" ".$number[$crores]." ".$number[$crores_odd]." ".$number[10000000];
}

$tens_old=(int)($numb%10000000);
$str.=" ".$this->upto_9999999($tens_old);

return " ".$str;

}

function upto_9999999($numb){
$number= $this->number;
if($numb<=2000000){
$lakhs=(int)($numb/100000);
$str=" ".$number[$lakhs]." ".$number[100000];
}else{
$lakhs=(int)($numb/1000000)*10;
$lakhs_odd=(int)($numb%1000000)/100000;
$str=" ".$number[$lakhs]." ".$number[$lakhs_odd]." ".$number[100000];
}

$tens_old=(int)($numb%100000);
$str.=" ".$this->upto_99999($tens_old);

return " ".$str;

}
function upto_99999($numb){
$number= $this->number;
if($numb<=20000){
$thousands=(int)($numb/1000);
$str=" ".$number[$thousands]." ".$number[1000];
}else{
$thousands=(int)($numb/10000)*10;
$thousands_odd=(int)($numb%10000)/1000;
$str=" ".$number[$thousands]." ".$number[$thousands_odd]." ".$number[1000];
}

$tens_old=(int)($numb%1000);
$str.=" ".$this->upto_999($tens_old);

return " ".$str;

}

function upto_999($numb){
$number= $this->number;
if($numb>99 && $numb<1000){
$hundreds=(int)($numb/100);
$str=" ".$number[$hundreds]." ".$number[100];
$tens_old=(int)($numb%100);
$str.=" ".$this->upto_99($tens_old);

return " ".$str;
}

}
function upto_99($numb){
$number= $this->number;
if($numb>20 && $numb<100){
$tens=(int)($numb/10)*10;
$units=$numb%10;
return $number[$tens]." ".$number[$units];
}
else{
return " ".$number[$numb];
}
}
}
$leter=new N2L();

echo "The number ".$_POST['number']." in Words is ".$leter->index($_POST['number']);

}

?>

How to convert some character into numeric in php?

Use ord() to return the ascii value. Subtract 96 to return a number where a=1, b=2....

Upper and lower case letters have different ASCII values, so if you want to handle them the same, you can use strtolower() to convert upper case to lower case.

To handle the NULL case, simply use if($dest). This will be true if $dest is something other than NULL or 0.

PHP is a loosely typed language, so there is no need to declare the types. So char dest='a'; is incorrect. Variables have $ prefix in PHP and no type declaration, so it should be $dest = 'a';.

Live Example

<?php

function toNumber($dest)
{
if ($dest)
return ord(strtolower($dest)) - 96;
else
return 0;
}

// Let's test the function...
echo toNumber(NULL) . " ";
echo toNumber('a') . " ";
echo toNumber('B') . " ";
echo toNumber('c');

// Output is:
// 0 1 2 3
?>

PS:
You can look at the ASCII values here.



Related Topics



Leave a reply



Submit