【问题标题】:PHP increase word (one -> two -> three) [closed]PHP加字(一->二->三)[关闭]
【发布时间】:2013-09-03 14:00:58
【问题描述】:

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

我需要做到这一点:

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

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

但是我需要使用 PHP 将值从 one 增加到 two 我知道你可以进行数字增量但是,我不知道如何做到这一点查看数组?

【问题讨论】:

  • 为什么不能使用element1element2?如果它确实需要您使用onetwo 等,那是一个非常设计不佳的插件......没有办法做你想做的事,你可以' t 增加字数。您必须存储一个大数组,或者编写/查找自定义函数来将任意数字转换为单词。
  • 呃?发布您的数组并更好地定义您想要实现的目标将对您有所帮助
  • 使用数组有什么问题?阅读此链接,该链接将数字转换为等效单词:karlrixon.co.uk/writing/convert-numbers-to-words-with-php

标签: php


【解决方案1】:

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

【讨论】:

  • 接下来的问题是......值得麻烦吗?
  • 正如“Martin Perry”所说,使用数字可能更容易。在这里大声喊叫!
  • 我完全同意,并建议不要使用我提供的示例,但此功能确实可以满足问题的要求。也许它会对将来的某人有所帮助。干杯!
【解决方案2】:

没有数组查找就无法转换 1 => "one", 2 => "two"。你应该将你的元素命名为element#number

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

【讨论】:

  • 谢谢!你将如何进行编码?我想我最多需要达到 30 次...
  • @Neil 我会使用 for-loop... 但你的问题不太好理解
【解决方案3】:

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

底线:你不能这样做,除非你从数组、对象等中读取数字字符串...

【讨论】:

    猜你喜欢
    • 2016-02-24
    • 1970-01-01
    • 2014-09-08
    • 2017-02-21
    • 2012-06-04
    • 1970-01-01
    • 2016-05-02
    • 1970-01-01
    • 2021-04-09
    相关资源
    最近更新 更多