PHP:使用单词增加计数器功能(即第一,第二,第三等)

Vic*_*rez 9 php counter ordinals increment cpu-word

我一直试图找到一个使用单词递增计数器的函数.我知道可能使用带后缀的数字(即第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';
}
Run Code Online (Sandbox Code Playgroud)

代码来源

但有没有办法用文字复制它(即第一,第二,第三等......)?

我希望创建一个无限的计数器是非常困难的(但并非不可能),但是任何高达20的计数器就足够了.

任何帮助将非常感激.

SIF*_*IFE 9

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);

?>
Run Code Online (Sandbox Code Playgroud)

来源.

  • 这可以产生"一,二,三......"而不是"第一,第二,第三......" (8认同)

Par*_*ney 7

二十并不是硬编码.你只需要一个数组,而不是一个函数.

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

foreach ($array as $key => $value)
{
  echo "$value index is $key";
}
Run Code Online (Sandbox Code Playgroud)

更直接的答案是:没有内置功能可以满足您的需求.


Hyp*_*eus 5

这里有一些伪代码可能以一种希望的好方法进行了引导:

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'
Run Code Online (Sandbox Code Playgroud)

  • 当然,“11st”和“12nd”和“13rd”看起来不太对劲。我想你可以为他们做具体的例外。 (2认同)

小智 5

<?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";
  }
}



?>
Run Code Online (Sandbox Code Playgroud)