Converting a number (1, 2, 3) to a string (one, two, three) in PHP
pear has a package Numbers_Words:
$numberToWord = new Numbers_Words();
echo $numberToWords->toWords(200);
convert number(1,2,3) to string(one,two,three) Cake PHP
Use this:
$num = 4263;
$numbers10 = array('ten','twenty','thirty','fourty','fifty','sixty','seventy','eighty','ninety');
$numbers01 = array('one','two','three','four','fife','six','seven','eight','nine','ten',
'eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen');
if($num == 0) {
echo "zero";
}
$thousands = floor($num/1000);
if($thousands != 0) {
echo $numbers01[$thousands-1] . " thousand ";
$num -= $thousands*1000;
}
$hundreds = floor($num/100);
if($hundreds != 0) {
echo $numbers01[$hundreds-1] . " hundred ";
$num -= $hundreds*100;
}
if($num < 20) {
if($num != 0) {
echo $numbers01[$num-1];
}
} else {
$tens = floor($num/10);
echo $numbers10[$tens-1] . " ";
$num -= $tens*10;
if($num != 0) {
echo $numbers01[$num-1];
}
}
Output:
four thousand two hundred sixty three
PHP increase word (one - two - three)
There is an example in this documentation.
function int_to_words($x) {
global $nwords;
if(!is_numeric($x))
$w = '#';
else if(fmod($x, 1) != 0)
$w = '#';
else {
if($x < 0) {
$w = 'minus ';
$x = -$x;
} else
$w = '';
// ... now $x is a non-negative integer.
if($x < 21) // 0 to 20
$w .= $nwords[$x];
else if($x < 100) { // 21 to 99
$w .= $nwords[10 * floor($x/10)];
$r = fmod($x, 10);
if($r > 0)
$w .= '-'. $nwords[$r];
} else if($x < 1000) { // 100 to 999
$w .= $nwords[floor($x/100)] .' hundred';
$r = fmod($x, 100);
if($r > 0)
$w .= ' and '. int_to_words($r);
} else if($x < 1000000) { // 1000 to 999999
$w .= int_to_words(floor($x/1000)) .' thousand';
$r = fmod($x, 1000);
if($r > 0) {
$w .= ' ';
if($r < 100)
$w .= 'and ';
$w .= int_to_words($r);
}
} else { // millions
$w .= int_to_words(floor($x/1000000)) .' million';
$r = fmod($x, 1000000);
if($r > 0) {
$w .= ' ';
if($r < 100)
$word .= 'and ';
$w .= int_to_words($r);
}
}
}
return $w;
}
Number to String Value in php
It's easy.
$cypher = array('zero','one','two','three', 'four', 'five', 'six', 'seven', 'eight', 'nine');
$i = 9;
echo $cypher[$i];
PHP: increment counter function using words (i.e. First, Second, Third, etc.. )
There is a class from PEAR package can do that:
<?php
// include class
include("Numbers/Words.php");
// create object
$nw = new Numbers_Words();
// convert to string
echo "600 in words is " . $nw->toWords(600);
?>
Source.
Convert a number to its string representation
for the first option (spell out digits), strtr is your friend
$words = array(
'-' => 'minus ',
'1' => 'one ',
'2' => 'two ',
etc....
);
echo strtr(-123, $words);
How to convert number into word
Can you try this one.
function createFullWordOrdinal($number)
{
$ord1 = array(1 => "first", 2 => "second", 3 => "third", 5 => "fifth", 8 => "eight", 9 => "ninth", 11 => "eleventh", 12 => "twelfth", 13 => "thirteenth", 14 => "fourteenth", 15 => "fifteenth", 16 => "sixteenth", 17 => "seventeenth", 18 => "eighteenth", 19 => "nineteenth");
$num1 = array("zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eightteen", "nineteen");
$num10 = array("zero", "ten", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety");
$places = array(2 => "hundred", 3 => "thousand", 6 => "million", 9 => "billion", 12 => "trillion", 15 => "quadrillion", 18 => "quintillion", 21 => "sextillion", 24 => "septillion", 27 => "octillion");
$number = array_reverse(str_split($number));
if ($number[0] == 0)
{
if ($number[1] >= 2)
$out = str_replace("y", "ieth", $num10[$number[1]]);
else
$out = $num10[$number[1]]."th";
}
else if (isset($number[1]) && $number[1] == 1)
{
$out = $ord1[$number[1] . $number[0]];
}
else
{
if (array_key_exists($number[0], $ord1))
$out = $ord1[$number[0]];
else
$out = $num1[$number[0]]."th";
}
if((isset($number[0]) && $number[0] == 0) || (isset($number[1]) && $number[1] == 1))
{
$i = 2;
}
else
{
$i = 1;
}
while ($i < count($number))
{
if ($i == 1)
{
$out = $num10[$number[$i]] . " " . $out;
$i++;
}
else if ($i == 2)
{
$out = $num1[$number[$i]] . " hundred " . $out;
$i++;
}
else
{
if (isset($number[$i + 2]))
{
$tmp = $num1[$number[$i + 2]] . " hundred ";
$tmpnum = $number[$i + 1].$number[$i];
if ($tmpnum < 20)
$tmp .= $num1[$tmpnum] . " " . $places[$i] . " ";
else
$tmp .= $num10[$number[$i + 1]] . " " . $num1[$number[$i]] . " " . $places[$i] . " ";
$out = $tmp . $out;
$i+=3;
}
else if (isset($number[$i + 1]))
{
$tmpnum = $number[$i + 1].$number[$i];
if ($tmpnum < 20)
$out = $num1[$tmpnum] . " " . $places[$i] . " " . $out;
else
$out = $num10[$number[$i + 1]] . " " . $num1[$number[$i]] . " " . $places[$i] . " " . $out;
$i+=2;
}
else
{
$out = $num1[$number[$i]] . " " . $places[$i] . " " . $out;
$i++;
}
}
}
return $out;
}
This will give you the output you want.
createFullWordOrdinal(1) ----> first
createFullWordOrdinal(2) ----> second
createFullWordOrdinal(3) ----> third
createFullWordOrdinal(4) ----> fourth
Is there an easy way to convert a number to a word in PHP?
I found some (2007/2008) source-code online and as it is copyright but I can use it freely and modify it however I want, so I place it here and re-license under CC-Wiki:
<?php
/**
* English Number Converter - Collection of PHP functions to convert a number
* into English text.
*
* This exact code is licensed under CC-Wiki on Stackoverflow.
* http://creativecommons.org/licenses/by-sa/3.0/
*
* @link http://stackoverflow.com/q/277569/367456
* @question Is there an easy way to convert a number to a word in PHP?
*
* This file incorporates work covered by the following copyright and
* permission notice:
*
* Copyright 2007-2008 Brenton Fletcher. http://bloople.net/num2text
* You can use this freely and modify it however you want.
*/
function convertNumber($number)
{
list($integer, $fraction) = explode(".", (string) $number);
$output = "";
if ($integer{0} == "-")
{
$output = "negative ";
$integer = ltrim($integer, "-");
}
else if ($integer{0} == "+")
{
$output = "positive ";
$integer = ltrim($integer, "+");
}
if ($integer{0} == "0")
{
$output .= "zero";
}
else
{
$integer = str_pad($integer, 36, "0", STR_PAD_LEFT);
$group = rtrim(chunk_split($integer, 3, " "), " ");
$groups = explode(" ", $group);
$groups2 = array();
foreach ($groups as $g)
{
$groups2[] = convertThreeDigit($g{0}, $g{1}, $g{2});
}
for ($z = 0; $z < count($groups2); $z++)
{
if ($groups2[$z] != "")
{
$output .= $groups2[$z] . convertGroup(11 - $z) . (
$z < 11
&& !array_search('', array_slice($groups2, $z + 1, -1))
&& $groups2[11] != ''
&& $groups[11]{0} == '0'
? " and "
: ", "
);
}
}
$output = rtrim($output, ", ");
}
if ($fraction > 0)
{
$output .= " point";
for ($i = 0; $i < strlen($fraction); $i++)
{
$output .= " " . convertDigit($fraction{$i});
}
}
return $output;
}
function convertGroup($index)
{
switch ($index)
{
case 11:
return " decillion";
case 10:
return " nonillion";
case 9:
return " octillion";
case 8:
return " septillion";
case 7:
return " sextillion";
case 6:
return " quintrillion";
case 5:
return " quadrillion";
case 4:
return " trillion";
case 3:
return " billion";
case 2:
return " million";
case 1:
return " thousand";
case 0:
return "";
}
}
function convertThreeDigit($digit1, $digit2, $digit3)
{
$buffer = "";
if ($digit1 == "0" && $digit2 == "0" && $digit3 == "0")
{
return "";
}
if ($digit1 != "0")
{
$buffer .= convertDigit($digit1) . " hundred";
if ($digit2 != "0" || $digit3 != "0")
{
$buffer .= " and ";
}
}
if ($digit2 != "0")
{
$buffer .= convertTwoDigit($digit2, $digit3);
}
else if ($digit3 != "0")
{
$buffer .= convertDigit($digit3);
}
return $buffer;
}
function convertTwoDigit($digit1, $digit2)
{
if ($digit2 == "0")
{
switch ($digit1)
{
case "1":
return "ten";
case "2":
return "twenty";
case "3":
return "thirty";
case "4":
return "forty";
case "5":
return "fifty";
case "6":
return "sixty";
case "7":
return "seventy";
case "8":
return "eighty";
case "9":
return "ninety";
}
} else if ($digit1 == "1")
{
switch ($digit2)
{
case "1":
return "eleven";
case "2":
return "twelve";
case "3":
return "thirteen";
case "4":
return "fourteen";
case "5":
return "fifteen";
case "6":
return "sixteen";
case "7":
return "seventeen";
case "8":
return "eighteen";
case "9":
return "nineteen";
}
} else
{
$temp = convertDigit($digit2);
switch ($digit1)
{
case "2":
return "twenty-$temp";
case "3":
return "thirty-$temp";
case "4":
return "forty-$temp";
case "5":
return "fifty-$temp";
case "6":
return "sixty-$temp";
case "7":
return "seventy-$temp";
case "8":
return "eighty-$temp";
case "9":
return "ninety-$temp";
}
}
}
function convertDigit($digit)
{
switch ($digit)
{
case "0":
return "zero";
case "1":
return "one";
case "2":
return "two";
case "3":
return "three";
case "4":
return "four";
case "5":
return "five";
case "6":
return "six";
case "7":
return "seven";
case "8":
return "eight";
case "9":
return "nine";
}
}
Related Topics
Any Way to Return PHP 'Json_Encode' With Encode Utf-8 and Not Unicode
How to Replace Text Urls and Exclude Urls in HTML Tags
PHP Expression ≪≪≪Eob
How to Re-Index the Values of an Array in PHP
PHP Get Values from Json Encode
Remove Warning Messages in PHP
How to Get First 5 Characters from String
Redirecting to Previous Page After Login - PHP
How to Group a Multidimensional Array by a Particular Subarray Value
Group Array by Subarray Values
PHP 5 Strpos() Difference Between Returning 0 and False