是否有一個PHP函數的字符串,如 '十' 轉換爲整數?
如果沒有,那麼你會怎麼做呢?
已嘗試谷歌和php.net搜索沒有成功
1
A
回答
6
這裏是概念類的證明,我剛寫做到這一點...檢查出來:
class WordToNumber {
public $modifiers = array(
'hundred' => 100,
);
public $negatives = array(
'minus' => true,
'negative' => true,
);
public $numbers = array(
'zero' => 0,
'one' => 1,
'two' => 2,
'three' => 3,
'four' => 4,
'five' => 5,
'six' => 6,
'seven' => 7,
'eight' => 8,
'nine' => 9,
'ten' => 10,
'eleven' => 11,
'twelve' => 12,
'thirteen' => 13,
'fourteen' => 14,
'fifteen' => 15,
'sixteen' => 16,
'seventeen' => 17,
'eighteen' => 18,
'nineteen' => 19,
'twenty' => 20,
'thirty' => 30,
'forty' => 40,
'fifty' => 50,
'sixty' => 60,
'seventy' => 70,
'eighty' => 80,
'ninety' => 90,
);
public $powers = array(
'thousand' => 1000,
'million' => 1000000,
'billion' => 1000000000,
);
public function __construct() {
}
public function parse($string) {
$string = $this->prepare($string);
$parts = preg_split('#\s+#', $string, -1, PREG_SPLIT_NO_EMPTY);
$buffer = 0;
$lastPower = 1;
$powers = array(
1 => 0,
);
$isNegative = false;
foreach ($parts as $part) {
if (isset($this->negatives[$part])) {
$isNegative = true;
} elseif (isset($this->numbers[$part])) {
$buffer += $this->numbers[$part];
} elseif (isset($this->modifiers[$part])) {
$buffer *= $this->modifiers[$part];
} elseif (isset($this->powers[$part])) {
if ($buffer == 0) {
//Modify last power
$buffer = $powers[$lastPower];
unset($powers[$lastPower]);
$power = $lastPower * $this->powers[$part];
$powers[$power] = $buffer;
$lastPower = $power;
$buffer = 0;
} else {
$powers[$this->powers[$part]] = $buffer;
$buffer = 0;
$lastPower = $this->powers[$part];
}
} else {
throw new LogicException('Unknown Token Found: '.$part);
}
}
if (!empty($buffer)) {
$powers[1] = $buffer;
}
$total = 0;
foreach ($powers as $power => $sub) {
$total += $power * $sub;
}
if ($isNegative) {
$total *= -1;
}
return $total;
}
protected function prepare($string) {
$string = preg_replace('#(\s+|-|\band\b)#i', ' ', $string);
$string = mb_convert_case($string, MB_CASE_LOWER);
return $string;
}
}
和測試:
$parser = new WordToNumber();
$strings = array(
'one hundred and fifty two',
'fifteen',
'one thousand million',
'four hundred thousand five hundred fourty three',
'fifteen hundred',
'one thousand twelve hundred',
'negative two',
'minus three hundred and fifty seven thousand four hundred and two',
);
foreach ($strings as $str) {
echo $parser->parse($str).' - '.$str."\n";
}
結果:
152 - one hundred and fifty two
15 - fifteen
1000000000 - one thousand million
400543 - four hundred thousand five hundred fourty three
1500 - fifteen hundred
2200 - one thousand twelve hundred
-2 - negative two
-357402 - minus three hundred and fifty seven thousand four hundred and two
它不支持大數字(因此爲什麼十億是最大的功率),但它應該進行簡單修改,以支持他們通過使用bcmath擴展...如果你想,我可以快速修改它以處理數字儘可能高。
1
您可以使用關聯數組(即哈希)來包含您所期望的值。例如:
$assoc_arr = array("one" => 1, "ten" => 10);
$my_ans = $assoc_arr["ten"] + 5;
+0
這將是最好的一個簡單的解決方案,即如果你只必須轉換一些價值觀。如果你想將任意字符串轉換爲數字,你必須從定義語法開始。 – user318904 2010-11-15 22:03:30
+0
沒錯,但不清楚答案的複雜程度。 – GreenMatt 2010-11-15 22:23:20
2
function smallTextIntToDigits($x)
{
$lilx = lcase($x)
$data = array('one'=>1,'two'=>2,'three'=>3,'four'=>4,'five'=>5,'six'=>6,'seven'=>7,'eight'=>8,'nine'=>9,'ten'=>10,'eleven'=>11,'twelve'=>12,'thirteen'=>13,'fourteen'=>14,'fifteen'=>15);
if (isset($data[$lilx]))
return $data[$lilx];
elseif (isset(data[lcase(str_replace('teen', '', $x))])
return data[(str_replace('teen', '', lcase($x))] + 10;
return -1; //harder stuff, recurse on big ones. blah blah blah
}
相關問題
- 1. 將10位數字轉換爲十六進制字符串
- 2. 轉換4字節十六進制字符串爲整數
- 3. 將字符串轉換爲整數
- 4. 將字符串轉換爲整數?
- 5. 將整數十六進制格式轉換爲字符串
- 6. 將十六進制字符串轉換爲整數
- 7. 將特定的字符串/整數轉換爲十進制
- 8. 十六進制字符串轉換爲整數在C
- 9. 字符串轉換爲十六進制整數用C
- 10. 將整數轉換爲十六進制字符串
- 11. 在JavaScript中將整數轉換爲十六進制字符串
- 12. 在Groovy中將整數轉換爲十六進制字符串
- 13. SQL:將整數轉換爲十六進制字符串?
- 14. 將十六進制字符串轉換爲整數
- 15. 在Ruby中將整數轉換爲十六進制字符串
- 16. 將十六進制字符串轉換爲整數
- 17. 轉換十進制到一般的整數作爲字符串
- 18. C++如何將十六進制整數轉換爲字符串?
- 19. 將十六進制字符串轉換爲整數
- 20. 如何將十六進制整數轉換爲字符串
- 21. 將字符串轉換爲Python中的十進制整數
- 22. 如何將'整數'字符串轉換爲'十六進制'字符串C
- 23. 將字符串轉換爲Java中的10位整數
- 24. 轉換字符串數組爲整數
- 25. 如何將表示爲十進制字符串的64位整數轉換爲十六進制字符串
- 26. 十進制數字符串轉換爲十六進制的字符串
- 27. 將字符串轉換爲整數
- 28. 整數轉換爲字符串?
- 29. Swift:將字符串轉換爲整數
- 30. 將字符串轉換爲整數
可能的重複[在PHP中將單詞轉換爲數字](http://stackoverflow.com/questions/1077600/converting-words-to-numbers-in-php) - 這裏有兩個很好的參考。第一個是你自己實現一個算法,另一個是預先構建的工具... – ircmaxell 2010-11-15 21:59:39
我實際上認爲php有一個函數用於將字符串轉換爲數字,因爲它是一種非常強大的語言。如果沒有內置函數,那麼我認爲這不值得。 – andrew 2010-11-15 22:24:21
你在想Python。 – outis 2010-11-15 22:40:10