2011-05-24 87 views
8

我一直在試圖找到一個函數使用單詞增加計數器。我知道它可能使用帶後綴的數字(即1st,2nd,3rd等)。下面是代碼片段我有:PHP:使用字增加計數器功能(即第一,第二,第三等)

function addOrdinalNumberSuffix($num) { 
    if (!in_array(($num % 100),array(11,12,13))){ 
     switch ($num % 10) { 
      // Handle 1st, 2nd, 3rd 
      case 1: return $num.'st'; 
      case 2: return $num.'nd'; 
      case 3: return $num.'rd'; 
     } 
    } 
    return $num.'th'; 
} 

Code Source

但是,有沒有辦法用言語來複制這個(即第一,第二,第三,等等)?我希望創建一個無限計數器是相當困難的(但不是不可能的),但是任何高達20的計數器就足夠了。

任何幫助將不勝感激。

回答

8

有一個class從PEAR包可以做到這一點:

<?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

+0

肯定會嘗試一下。感謝SIFE。 – VicePrez 2011-05-24 20:05:57

+0

@VicePrez很高興爲您效勞。 – SIFE 2011-05-24 20:17:05

+6

這可以生成「一,二,三...」,但不是「第一,第二,第三......」 – 2011-08-09 21:12:30

6

二十是沒有太多硬編碼。你只需要一個數組,而不是一個函數。

$array = array('First', 'Second', 'Third', ...); 

foreach ($array as $key => $value) 
{ 
    echo "$value index is $key"; 
} 

更直接的答案是:沒有一個內置的功能,做你在找什麼。

+0

1爲簡潔和簡單。 – VicePrez 2011-05-24 20:05:31

+0

就是這樣:然後他可以說'你是$ array [$ number] visitor''。 – 2014-08-23 10:07:03

4

這裏去一些僞代碼,在希望好辦法也許導致:

input = //any number 
output = string (input) 
if output ends with '1' then output += 'st' 
else if output ends with '2' then output += 'nd' 
else if output ends with '3' then output += 'rd' 
else output += 'th' 
+0

這有點類似於我所做的。感謝您的幫助。 – VicePrez 2011-05-24 20:07:16

+0

當然「第11」,「第12」和「第13」看起來不太正確。我想你可以爲他們制定特定的例外。 – 2018-03-02 23:28:02

3

序數(僅英文)的基礎上,SIFE的回答是:

include("Numbers/Words.php"); 

function Nth($n) 
{ 
    $nw = new Numbers_Words(); 
    $s = $nw->toWords($n); 
    $replacements = array(
     'one' => 'first', 
     'two' => 'second', 
     'three' => 'third', 
     've' => 'fth', 
     't' => 'th', 
     'e' => 'th', 
     'y' => 'ieth', 
     '' => 'th', 
    ); 
    foreach ($replacements as $from => $to) 
    { 
     $count = 0; 
     $r = preg_replace('/' . $from . '$/', $to, $s, 1, $count); 
     if ($count) 
      return $r; 
    } 
} 
2

下面是一個簡單的PHP鏈接功能,顯示瞭如何以簡單的方式處理這個問題: http://webdeveloperblog.tiredmachine.com/php-converting-an-integer-123-to-ordinal-word-firstsecondthird/

所提供的示例僅適用到五十年代,但可以很容易地在擴展噸o達到更高的範圍。

function numToOrdinalWord($num) 
{ 
    $first_word = array('eth','First','Second','Third','Fouth','Fifth','Sixth','Seventh','Eighth','Ninth','Tenth','Elevents','Twelfth','Thirteenth','Fourteenth','Fifteenth','Sixteenth','Seventeenth','Eighteenth','Nineteenth','Twentieth'); 
    $second_word =array('','','Twenty','Thirthy','Forty','Fifty'); 

    if($num <= 20) 
     return $first_word[$num]; 

    $first_num = substr($num,-1,1); 
    $second_num = substr($num,-2,1); 

    return $string = str_replace('y-eth','ieth',$second_word[$second_num].'-'.$first_word[$first_num]); 
} 
3
<?php 

    /*****************************************************************************/ 

function ToOrdinal($n) { 
    /* Convert a cardinal number in the range 0 - 999 to an ordinal in 
    words. */ 

    /* The ordinal will be collected in the variable $ordinal. 
    Initialize it as an empty string.*/ 
    $ordinal = ""; 

    /* Check that the number is in the permitted range. */ 
    if ($n >= 0 && $n <= 999) 
    null; 
    else{ 
    echo "<br />You have called the function ToOrdinal with this value: $n, but 
it is not in the permitted range, from 0 to 999, inclusive.<br />"; 
    return; 
    } 
    /* Extract the units. */ 
    $u = $n % 10; 

    /* Extract the tens. */ 
    $t = floor(($n/10) % 10); 

    /* Extract the hundreds. */ 
    $h = floor($n/100); 

    /* Determine the hundreds */ 
    if ($h > 0) { 

    /* ToCardinalUnits() works with numbers from 0 to 9, so it's okay 
     for finding the number of hundreds, which must lie within this 
     range. */ 
    $ordinal .= ToCardinalUnits($h); 
    $ordinal .= " hundred"; 

    /* If tens and units are zero, append "th" and quit */ 
    if ($t == 0 && $u == 0) { 
     $ordinal .= "th"; 
    } else { 
     /* Otherwise put in a blank space to separate the hundreds from 
    what follows. */ 
     $ordinal .= " "; 
    } 
    } 

    /* Determine the tens, unless there is just one ten. If units are 0, 
    handle them separately */ 
    if ($t >= 2 && $u != 0) { 
    switch ($t) { 
    case 2: 
     $ordinal .= "twenty-"; 
     break; 
    case 3: 
     $ordinal .= "thirty-"; 
     break; 
    case 4: 
     $ordinal .= "forty-"; 
     break; 
    case 5: 
     $ordinal .= "fifty-"; 
     break; 
    case 6: 
     $ordinal .= "sixty-"; 
     break; 
    case 7: 
     $ordinal .= "seventy-"; 
     break; 
    case 8: 
     $ordinal .= "eighty-"; 
     break; 
    case 9: 
     $ordinal .= "ninety-"; 
     break; 
    } 
    } 
    /* Print the tens (unless there is just one ten) with units == 0 */ 
    if ($t >= 2 && $u == 0) { 
    switch ($t) { 
    case 2: 
     $ordinal .= "twentieth"; 
     break; 
    case 3: 
     $ordinal .= "thirtieth"; 
     break; 
    case 4: 
     $ordinal .= "fortieth"; 
     break; 
    case 5: 
     $ordinal .= "fiftieth"; 
     break; 
    case 6: 
     $ordinal .= "sixtieth"; 
     break; 
    case 7: 
     $ordinal .= "seventieth"; 
     break; 
    case 8: 
     $ordinal .= "eightieth"; 
     break; 
    case 9: 
     $ordinal .= "ninetieth"; 
     break; 
    } 
    } 


    /* Print the teens, if the tens is 1. */ 
    if ($t == 1) { 
    switch ($u) { 
    case 0: 
     $ordinal .= "tenth"; 
     break; 
    case 1: 
     $ordinal .= "eleventh"; 
     break; 
    case 2: 
     $ordinal .= "twelfth"; 
     break; 
    case 3: 
     $ordinal .= "thirteenth"; 
     break; 
    case 4: 
     $ordinal .= "fourteenth"; 
     break; 
    case 5: 
     $ordinal .= "fifteenth"; 
     break; 
    case 6: 
     $ordinal .= "sixteenth"; 
     break; 
    case 7: 
     $ordinal .= "seventeenth"; 
     break; 
    case 8: 
     $ordinal .= "eighteenth"; 
     break; 
    case 9: 
     $ordinal .= "nineteenth"; 
     break; 
    } 
    } 

    /* Print the units. */ 
    if ($t != 1) { 
    switch ($u) { 
    case 0: 
     if ($n == 0) 
    $ordinal .= "zeroth"; 
     break; 
    case 1: 
     $ordinal .= "first"; 
     break; 
    case 2: 
     $ordinal .= "second"; 
     break; 
    case 3: 
     $ordinal .= "third"; 
     break; 
    case 4: 
     $ordinal .= "fourth"; 
     break; 
    case 5: 
     $ordinal .= "fifth"; 
     break; 
    case 6: 
     $ordinal .= "sixth"; 
     break; 
    case 7: 
     $ordinal .= "seventh"; 
     break; 
    case 8: 
     $ordinal .= "eighth"; 
     break; 
    case 9: 
     $ordinal .= "ninth"; 
     break; 
    } 
    } 
    return $ordinal; 
} 

/*****************************************************************************/ 


function ToCardinalUnits($n) { 
    /* Convert a number in the range 0 to 9 into its word equivalent. */ 

    /* Make sure the number is in the permitted range. */ 
    if ($n >= 0 && $n <= 9) 
    null; 
    else 
    { 
     echo "<br />You have called ToCardinal() with an argument $n, but the permitted range is 0 to 9, inclusive.<br />"; 
    } 

    switch ($n) { 
    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"; 
    } 
} 



?> 
相關問題