PHP - Convert Number into Word

PHP

Nicesnippets

326

07-10-2020


PHP - Convert Number into Word

Hello Friends,

Now let's see example of how to convert number into word using php. i am going to learn you convert number to word in php. we will show number into word convert using php. This tutorial will give you how to convert number to word in php.

In this blog, i will show you convert number into word in php. Here i will give you full example for convert number into word using php.

In this example i am create form in one input to enter your number into convert in word using php. So let's see the bellow example:

Example :

<!DOCTYPE html>

<html>

<head>

<title>How to Convert Number into Words in PHP - NiceSnippets.com</title>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha512-MoRNloxbStBcD8z3M/2BmnT+rg4IsMxPkXaGh2zD6LGNNFE80W3onsAhRcMAMrSoyWL9xD7Ert0men7vR8LUZg==" crossorigin="anonymous" />

</head>

<body>

<div class="row mt-5">

<div class="col-md-6 offset-3 mt-5">

<div class="card">

<div class="card-header bg-success text-white">

<h5><strong>How to Convert Number into Words in PHP - NiceSnippets.com</strong></h5>

</div>

<div class="card-body">

<form method="post">

<div class="form-group">

<label>Number</label>

<input type="text" name="text-field" class="form-control" placeholder="Enter Number" onchange="convertNumberToWords()" />

</div>

<div class="form-group text-center">

<button class="btn btn-success">Submit</button>

</div>

</form>

</div>

</div>

</div>

<div class="col-md-6 offset-3 mt-4">

<?php

$word = convertNumberToWordsForIndia($_POST['text-field']);

if (!empty($word)) {

echo '<div class="alert bg-primary text-white">'.$word.'</div>';

}

?>

</div>

</div>

<?php

function convertNumberToWordsForIndia($number){

$words = array(

'0'=> '' ,'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' => 'fouteen','15' => 'fifteen',

'16' => 'sixteen','17' => 'seventeen','18' => 'eighteen','19' => 'nineteen','20' => 'twenty',

'30' => 'thirty','40' => 'fourty','50' => 'fifty','60' => 'sixty','70' => 'seventy',

'80' => 'eighty','90' => 'ninty');

//First find the length of the number

$number_length = strlen($number);

//Initialize an empty array

$number_array = array(0,0,0,0,0,0,0,0,0);

$received_number_array = array();

//Store all received numbers into an array

for($i=0;$i<$number_length;$i++){

$received_number_array[$i] = substr($number,$i,1);

}

//Populate the empty array with the numbers received - most critical operation

for($i=9-$number_length,$j=0;$i<9;$i++,$j++){

$number_array[$i] = $received_number_array[$j];

}

$number_to_words_string = "";

//Finding out whether it is teen ? and then multiply by 10, example 17 is seventeen, so if 1 is preceeded with 7 multiply 1 by 10 and add 7 to it.

for($i=0,$j=1;$i<9;$i++,$j++){

//"01,23,45,6,78"

//"00,10,06,7,42"

//"00,01,90,0,00"

if($i==0 || $i==2 || $i==4 || $i==7){

if($number_array[$j]==0 || $number_array[$i] == "1"){

$number_array[$j] = intval($number_array[$i])*10+$number_array[$j];

$number_array[$i] = 0;

}

}

}

$value = "";

for($i=0;$i<9;$i++){

if($i==0 || $i==2 || $i==4 || $i==7){

$value = $number_array[$i]*10;

}

else{

$value = $number_array[$i];

}

if($value!=0) { $number_to_words_string.= $words["$value"]." "; }

if($i==1 && $value!=0){ $number_to_words_string.= "Crores "; }

if($i==3 && $value!=0){ $number_to_words_string.= "Lakhs "; }

if($i==5 && $value!=0){ $number_to_words_string.= "Thousand "; }

if($i==6 && $value!=0){ $number_to_words_string.= "Hundred & "; }

}

if($number_length>9){ $number_to_words_string = "Sorry This does not support more than 99 Crores"; }

return $number_to_words_string;

}

?>

</body>

</html>

it will help you...


Recommended Posts