PHP:使用字(即第一、第二、第三等)的递增计数器函数

PHP:使用字(即第一、第二、第三等)的递增计数器函数,php,word,counter,increment,ordinals,Php,Word,Counter,Increment,Ordinals,我一直在试图找到一个函数,它使用单词递增计数器。我知道可以使用带有后缀的数字(即1、2、3等)。下面是我得到的代码片段: function addOrdinalNumberSuffix($num) { if (!in_array(($num % 100),array(11,12,13))){ switch ($num % 10) { // Handle 1st, 2nd, 3rd case 1: return $num.'

我一直在试图找到一个函数,它使用单词递增计数器。我知道可以使用带有后缀的数字(即1、2、3等)。下面是我得到的代码片段:

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';
}

但是,有没有一种方法可以用单词(即第一、第二、第三等等)来复制这种情况呢

我认为创建一个无限计数器是相当困难的(但不是不可能的),但最多20个就足够了


任何帮助都将不胜感激。

20对于硬编码来说并不是那么重要。你只需要一个数组,而不是一个函数

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

foreach ($array as $key => $value)
{
  echo "$value index is $key";
}
更直接的答案是:没有一个内置函数可以满足您的需求。

有一个from 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);

?>


.

下面是一些伪代码,也许可以引导我们找到一个好方法:

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'
序号(仅限英文),基于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;
    }
}

下面是一个指向简单PHP函数的链接,该函数演示了如何以简单的方式处理此问题:

所提供的示例仅适用于50年代,但可以很容易地扩展到更高的范围

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]);
}
函数numToOrdinalWord($num)
{
$first_word=array('eth','first','Second','Third','Fouth','Fifth','Sixth','Seventh','sevents','thinth','Elevents','Seventh','thirth','thirth','thirth','Seventh','18hth','thirth','thirth';
$second_word=数组(“”、'二十’、'三十’、'四十’、'五十’);

如果($num
一个简短的小技巧来实现相同的结果,那么(ab)就用于它


不过最多只能使用31个,可能比使用帕里斯建议的数组要慢。

一定要尝试一下。谢谢SIFE。这可以生成“一、二、三…”但不能生成“一、二、三…”这与我的情况有些类似。谢谢你的帮助。当然可以是“十一”和“十二”以及“十三”看起来不太对。我想你可以为他们做一些特殊的例外。正是这样:然后他可以这样说:
你就是$array[$number]访问者
<?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";
  }
}



?>
$num = 2;
echo date("jS", strtotime("January {$num}"));

// Output
"2nd"