PHP增加单词(一个 - >两个 - >三个)

时间:2013-09-03 14:00:59

标签: php

我正在尝试在PHP中使用插件。

我需要实现这个目标:

<div id="elementone">
  <p>TEST</p>
</div>

<div id="elementwo">
  <p>TEST</p>
</div>

但是我需要使用PHP将值从一个增加到两个我知道你可以做数字增量然而,我不知道如何做到这一点除了看着阵列?

3 个答案:

答案 0 :(得分:2)

this文档中有一个示例。

 function int_to_words($x) {
global $nwords;

if(!is_numeric($x))
  $w = '#';
else if(fmod($x, 1) != 0)
  $w = '#';
else {
  if($x < 0) {
     $w = 'minus ';
     $x = -$x;
  } else
     $w = '';
  // ... now $x is a non-negative integer.

  if($x < 21)   // 0 to 20
     $w .= $nwords[$x];
  else if($x < 100) {   // 21 to 99
     $w .= $nwords[10 * floor($x/10)];
     $r = fmod($x, 10);
     if($r > 0)
        $w .= '-'. $nwords[$r];
  } else if($x < 1000) {   // 100 to 999
     $w .= $nwords[floor($x/100)] .' hundred';
     $r = fmod($x, 100);
     if($r > 0)
        $w .= ' and '. int_to_words($r);
  } else if($x < 1000000) {   // 1000 to 999999
     $w .= int_to_words(floor($x/1000)) .' thousand';
     $r = fmod($x, 1000);
     if($r > 0) {
        $w .= ' ';
        if($r < 100)
           $w .= 'and ';
        $w .= int_to_words($r);
     }
  } else {    //  millions
     $w .= int_to_words(floor($x/1000000)) .' million';
     $r = fmod($x, 1000000);
     if($r > 0) {
        $w .= ' ';
        if($r < 100)
           $word .= 'and ';
        $w .= int_to_words($r);
     }
  }
}
return $w;
}

答案 1 :(得分:1)

无法如何转换1 =&gt; “一”,2 =&gt;没有阵列查找的“两个”。你应该将你的元素命名为element#number

或者你可以使用它:Converting a number (1, 2, 3) to a string (one, two, three) in PHP

答案 2 :(得分:1)

谷歌有时非常方便。如果你想坚持那个相当糟糕的设计选择,请阅读这里:http://www.karlrixon.co.uk/writing/convert-numbers-to-words-with-php/

底线:除非您从数组,对象等中读取数字字符串,否则不能执行此操作...