Smarty
[ class tree: Smarty ] [ index: Smarty ] [ all elements ]

Source for file modifier.ordinal_suffix.php

Documentation is available at modifier.ordinal_suffix.php

  1. <?php
  2. /**
  3.  * Smarty plugin
  4.  * @package Smarty
  5.  * @subpackage plugins
  6.  */
  7.  
  8. /**
  9.  * Smarty plugin
  10.  * -------------------------------------------------------------
  11.  * Type:     modifier
  12.  * Name:     display_bytes
  13.  * Purpose:  show an integer in a human readable Byte size with optional resolution
  14.  * Example:  {$someFile|filesize|display_bytes:2}
  15.  * -------------------------------------------------------------
  16.  */
  17. function smarty_modifier_ordinal_suffix$pNum {
  18.  
  19.     // first convert to string if needed
  20.     $ret = (string) $pNum;
  21.     // now we grab the last digit of the number
  22.     $last_digit substr($ret-11);
  23.     // if the string is more than 2 chars long, we get
  24.     // the second to last character to evaluate
  25.     if (strlen($ret)>1{
  26.         $next_to_last substr($ret-21);
  27.     else {
  28.         $next_to_last "";
  29.     }
  30.     // now iterate through possibilities in a switch
  31.     switch($last_digit{
  32.         case "1":
  33.             // testing the second from last digit here
  34.             switch($next_to_last{
  35.                 case "1":
  36.                     $suffix ="th";
  37.                     break;
  38.                 default:
  39.                     $suffix ="st";
  40.             }
  41.             break;
  42.         case "2":
  43.             // testing the second from last digit here
  44.             switch($next_to_last{
  45.                 case "1":
  46.                     $suffix ="th";
  47.                     break;
  48.                 default:
  49.                     $suffix ="nd";
  50.             }
  51.             break;
  52.         // if last digit is a 3
  53.         case "3":
  54.             // testing the second from last digit here
  55.             switch($next_to_last{
  56.                 case "1":
  57.                     $suffix ="th";
  58.                     break;
  59.                 default:
  60.                     $suffix ="rd";
  61.             }
  62.             break;
  63.         // for all the other numbers we use "th"
  64.         default:
  65.             $suffix ="th";
  66.     }
  67.  
  68.     // finally, return our string with it's new suffix
  69.     return $pNum.tra$suffix );
  70.  
  71. }
  72. ?>

Documentation generated on Wed, 29 Jul 2015 13:57:09 +0000 by phpDocumentor 1.5.0-lsces