note 62478 deleted from function.number-format by crell
| From: | crell@php.net | Date: | Tue, 12 Apr 2022 01:08:42 +0000 |
| Subject: | note 62478 deleted from function.number-format by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-218214@lists.php.net to get a copy of this message | ||
Note Submitter: mikesabatino at gmail dot com
----
<?php
# Function to represent a number like '2nd', '10th', '101st' etc
function text_number($n)
{
# Array holding the teen numbers. If the last 2 numbers of $n are in this array, then we'll
add 'th' to the end of $n
$teen_array = array(11, 12, 13, 14, 15, 16, 17, 18, 19);
# Array holding all the single digit numbers. If the last number of $n, or if $n itself, is a key
in this array, then we'll add that key's value to the end of $n
$single_array = array(1 => 'st', 2 => 'nd', 3 => 'rd', 4
=> 'th', 5 => 'th', 6 => 'th', 7 => 'th', 8
=> 'th', 9 => 'th', 0 => 'th');
# Store the last 2 digits of $n in order to check if it's a teen number.
$if_teen = substr($n, -2, 2);
# Store the last digit of $n in order to check if it's a teen number. If $n is a single digit,
$single will simply equal $n.
$single = substr($n, -1, 1);
# If $if_teen is in array $teen_array, store $n with 'th' concantenated onto the end of
it into $new_n
if ( in_array($if_teen, $teen_array) )
{
$new_n = $n . 'th';
}
# $n is not a teen, so concant the appropriate value of it's $single_array key onto the end of
$n and save it into $new_n
elseif ( $single_array[$single] )
{
$new_n = $n . $single_array[$single];
}
# Return new
return $new_n;
}
?>