Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Change numerals to ordinals with PHP

  • This topic is empty.
Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • #43716
    chrisburton
    Participant

    Is there a way to change a dynamic set of numerals I’m using with a custom field (not WordPress) to ordinal?

    Example: 1, 2, 3, etc to first, second, third, etc.

    #129923
    TheDoc
    Member

    If you’re going to words, you’ll need to write some pretty complicated stuff. You can do a simple change like this: http://www.if-not-true-then-false.com/2010/php-1st-2nd-3rd-4th-5th-6th-php-add-ordinal-number-suffix/

    #129925
    chrisburton
    Participant

    What I’m trying to do is convert my already existing custom field that outputs a basic number (1, 2, 3, etc) and convert it to an ordinal. I was hoping there was a way much like `str_replace`.

    To output a basic numeral with my custom field, I just do the following:

    numeral)) ?>

    So figured I would have to use a variable to start off to change it into an ordinal:

    $ordinal = html($page->numeral));
    // not sure where to go from there exactly
    ?>

    #129928
    CrocoDillon
    Participant

    I found [this](http://www.phpro.org/examples/Convert-Numbers-to-Words.html).

    If `$page->numeral` is an integer, you can probably do (not sure what the html function does):

    numeral)); ?>

    Don’t think there’s a standard php function that does the same.

    #129929
    chrisburton
    Participant

    @CrocoDillon

    numeral)); ?>

    Outputs

    Fatal error: Call to undefined function convert_number()

    #129930
    CrocoDillon
    Participant

    Yeah, you would need to include the function from the link I gave you. Easiest way is to make a separate php file to past that function in and use require_once to include that file where ever you need the function.

    #129932
    chrisburton
    Participant

    @CrocoDillon Oops. Completely missed that link. Will try again.

    #129933
    CrocoDillon
    Participant

    Hmmm I just tried it and it’s not entirely what you’re after. I’ll look further.

    #129934
    chrisburton
    Participant

    @CrocoDillon Correct. It just outputs `one`, `two`, `three`, etc.

    #129935
    CrocoDillon
    Participant

    // Source: Wikipedia (http://en.wikipedia.org/wiki/Names_of_large_numbers)
    private static $scale = array(”, ‘thousand’, ‘million’, ‘billion’, ‘trillion’, ‘quadrillion’, ‘quintillion’, ‘sextillion’, ‘octillion’, ‘nonillion’, ‘decillion’, ‘undecillion’, ‘duodecillion’, ‘tredecillion’, ‘quattuordecillion’, ‘quindecillion’, ‘sexdecillion’, ‘septendecillion’, ‘octodecillion’, ‘noverndecillion’, ‘vigintillion’);
    private static $digit = array(”, ‘one’, ‘two’, ‘three’, ‘four’, ‘five’, ‘six’, ‘seven’, ‘eight’, ‘nine’, ‘ten’, ‘eleven’, ‘twelve’, ‘thirteen’, ‘fourteen’, ‘fifteen’, ‘sixteen’, ‘seventeen’, ‘eighteen’, ‘nineteen’);
    private static $digith = array(”, ‘first’, ‘second’, ‘third’, ‘fourth’, ‘fifth’, ‘sixth’, ‘seventh’, ‘eighth’, ‘ninth’, ‘tenth’, ‘eleventh’, ‘twelfth’, ‘thirteenth’, ‘fourteenth’, ‘fiftheenth’, ‘sixteenth’, ‘seventeenth’, ‘eighteenth’, ‘nineteenth’);
    private static $ten = array(”, ”, ‘twenty’, ‘thirty’, ‘fourty’, ‘fifty’, ‘sixty’, ‘seventy’, ‘eighty’, ‘ninety’);
    private static $tenth = array(”, ”, ‘twentieth’, ‘thirtieth’, ‘fortieth’, ‘fiftieth’, ‘sixtieth’, ‘seventieth’, ‘eightieth’, ‘ninetieth’);

    private static function floatToArray($number, &$int, &$frac) {
    // Forced $number as (string), effectively to avoid (float) inprecision
    @list(, $frac) = explode(‘.’, $number);
    if ($frac || !is_numeric($number) || (strlen($number) > 60)) throw new Exception(‘Not a number or not a supported number type’);
    // $int = explode(‘,’, number_format(ltrim($number, ‘0’), 0, ”, ‘,’)); — Buggy
    $int = str_split(str_pad($number, ceil(strlen($number)/3)*3, ‘0’, STR_PAD_LEFT), 3);
    }

    private static function thousandToEnglish($number) {
    // Gets numbers from 0 to 999 and returns the cardinal English
    $hundreds = floor($number / 100);
    $tens = $number % 100;
    $pre = ($hundreds ? self::$digit[$hundreds].’ hundred’ : ”);
    if ($tens < 20)
    $post = self::$digit[$tens];
    else
    $post = trim(self::$ten[floor($tens / 10)].’ ‘.self::$digit[$tens % 10]);
    if ($pre && $post) return $pre.’ and ‘.$post;
    return $pre.$post;
    }

    private static function cardinalToOrdinal($cardinal) {
    // Finds the last word in the cardinal arrays and replaces it with
    // the entry from the ordinal arrays, or appends “th”
    $words = explode(‘ ‘, $cardinal);
    $last = &$words[count($words)-1];
    if (in_array($last, self::$digit)) {
    $last = self::$digith[array_search($last, self::$digit)];
    } elseif (in_array($last, self::$ten)) {
    $last = self::$tenth[array_search($last, self::$ten)];
    } elseif (substr($last, -2) != ‘th’) {
    $last .= ‘th’;
    }
    return implode(‘ ‘, $words);
    }

    public static function toOrdinal($number) {
    // Converts a xth format number to English. e.g. 22nd to twenty-second.
    return trim(self::cardinalToOrdinal(self::toCardinal($number)));
    }

    public static function toCardinal($number) {
    // Converts a number to English. e.g. 22 to twenty-two.
    self::floatToArray($number, $int, $frac);
    $int = array_reverse($int);
    for($i=count($int)-1; $i>-1; $i–) {
    $englishnumber = self::thousandToEnglish($int[$i]);
    if ($englishnumber)
    $english[] = $englishnumber.’ ‘.self::$scale[$i];
    }
    $post = array_pop($english);
    $pre = implode(‘, ‘, $english);
    if ($pre && $post) return trim($pre.’ and ‘.$post);
    return trim($pre.$post);
    }
    }

    Put that in a separate php file instead. Use it like `numeral)); ?>`

    [source](http://www.codingforums.com/archive/index.php/t-180473.html)

    #129937
    chrisburton
    Participant

    It’s spitting out errors. So what I think I’m going to do, which is easier, is to create an additional custom field and manually add the words for every article.

    That way I can just do this to output what I need:

    ordinal) ?>

    #129938
    CrocoDillon
    Participant

    It worked for me :) But yeah that’s easier.

    #129939
    chrisburton
    Participant

    But thanks to you both for taking the time to help me out. Really appreciate it.

    @CrocoDillion @TheDoc

    #129940
    CrocoDillon
    Participant

    No problem!

    #240465
    faiswiz
    Participant
Viewing 15 posts - 1 through 15 (of 15 total)
  • The forum ‘Back End’ is closed to new topics and replies.