summaryrefslogtreecommitdiff
path: root/smartyplugins/modifier.ordinal_suffix.php
blob: 0215f17f8771d6f73ec28e19d0d87ba67b83ba4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
namespace Bitweaver\Plugins;

use Bitweaver\KernelTools;
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */

/**
 * Smarty plugin
 * -------------------------------------------------------------
 * Type:     modifier
 * Name:     display_bytes
 * Purpose:  show an integer in a human readable Byte size with optional resolution
 * Example:  {$someFile|filesize|display_bytes:2}
 * -------------------------------------------------------------
 */
function smarty_modifier_ordinal_suffix( $pNum ) {

	// first convert to string if needed
	$ret = (string) $pNum;
	// now we grab the last digit of the number
	$last_digit = substr($ret, -1, 1);
	// if the string is more than 2 chars long, we get
	// the second to last character to evaluate
	if (strlen($ret)>1) {
		$next_to_last = substr($ret, -2, 1);
	} else {
		$next_to_last = "";
	}
	// now iterate through possibilities in a switch
	switch($last_digit) {
		case "1":
			// testing the second from last digit here
			switch($next_to_last) {
				case "1":
					$suffix ="th";
					break;
				default:
					$suffix ="st";
			}
			break;
		case "2":
			// testing the second from last digit here
			switch($next_to_last) {
				case "1":
					$suffix ="th";
					break;
				default:
					$suffix ="nd";
			}
			break;
		// if last digit is a 3
		case "3":
			// testing the second from last digit here
			switch($next_to_last) {
				case "1":
					$suffix ="th";
					break;
				default:
					$suffix ="rd";
			}
			break;
		// for all the other numbers we use "th"
		default:
			$suffix ="th";
	}

	// finally, return our string with it's new suffix
	return $pNum.KernelTools::tra( $suffix );

}